Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' reset request and result vars
- Set httpRequest = New XMLHTTP60
- With ThisWorkbook.Sheets("Dashboard")
- ' clear the cells holding the response
- .Range("rngResponseSigRequestStatus").Value = vbNullString
- .Range("rngResponseSigRequestFull").Value = vbNullString
- ' define the new URL for the signature request using the Base URL returned aboved appended with '/envelopes'
- URL = .Range("rngResponseLoginBaseUrl").Value & "/envelopes"
- End With
- ' start the body of the request
- requestBody = "<envelopeDefinition xmlns=" & Chr(34) & "http://www.docusign.com/restapi" & Chr(34) & ">"
- ' add email info to the body
- requestBody = requestBody & "<emailSubject>API Call for adding signature request to document and sending</emailSubject>" & _
- "<status>sent</status>"
- ' add documents to the body
- requestBody = requestBody & "<documents>" & _
- "<document>" & _
- "<documentId>1</documentId>" & _
- "<name>" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value & "</name>" & _
- "</document>" & _
- "</documents>"
- ' add email recipients
- requestBody = requestBody & "<recipients>" & _
- "<signers>" & _
- "<signer>" & _
- "<recipientId>1</recipientId>" & _
- "<name>" & ThisWorkbook.Sheets("Dashboard").Range("rngRecipientName").Value & "</name>" & _
- "<email>" & ThisWorkbook.Sheets("Dashboard").Range("rngRecipientEmail").Value & "</email>" & _
- "<tabs>" & _
- "<signHereTabs>" & _
- "<signHere>" & _
- "<xPosition>100</xPosition>" & _
- "<yPosition>100</yPosition>" & _
- "<documentId>1</documentId>" & _
- "<pageNumber>1</pageNumber>" & _
- "</signHere>" & _
- "</signHereTabs>" & _
- "</tabs>" & _
- "</signer>" & _
- "</signers>" & _
- "</recipients>"
- ' close the envelope definition
- requestBody = requestBody & "</envelopeDefinition>"
- ' read in document in byte array
- Dim intFileNum%, bytTemp As Byte, intCellRow
- Dim bytTempAll As String
- intFileNum = FreeFile
- intCellRow = 0
- Open (ThisWorkbook.Path & "" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value) For Binary Access Read As intFileNum
- Do While Not EOF(intFileNum)
- ' intCellRow = intCellRow + 1
- Get intFileNum, , bytTemp
- 'Cells(intCellRow, 1) = bytTemp
- bytTempAll = bytTempAll & bytTemp
- Loop
- Close intFileNum
- Dim lenFile As Long
- lenFile = Len(bytTempAll) ' just curious
- ' read document in adobe stream
- Set AdobeStream = CreateObject("ADODB.STREAM")
- With AdobeStream
- .Type = 1 ' binary
- .Open ' open the file
- .LoadFromFile (ThisWorkbook.Path & "" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value)
- End With
- ' create multipart/form-data request
- Dim requestBodyPre As String
- requestBodyPre = vbCrLf & vbCrLf & "--BOUNDARY" & _
- vbCrLf & "Content-Type: application/xml" & _
- vbCrLf & "Content-Disposition: form-data" & vbCrLf & _
- vbCrLf & requestBody & _
- vbCrLf & vbCrLf & "--BOUNDARY" & _
- vbCrLf & "Content-Type: " & ThisWorkbook.Sheets("Dashboard").Range("rngContentType").Value & _
- vbCrLf & "Content-Disposition: file; filename=" & Chr(34) & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value & Chr(34) & "; documentid=1" & _
- vbCrLf
- ' break up the requestBody into 'Pre' and 'Post' because document bytes need to be passed in the middle
- Dim requestBodyPost As String
- requestBodyPost = vbCrLf & "--BOUNDARY--" & vbCrLf & vbCrLf
- ' open url
- Debug.Print "POSTing URL " & URL
- httpRequest.Open "POST", URL, False
- ThisWorkbook.Sheets("APICalls").Range("APICallSignatureREquest").Value = URL
- ' attach headers
- With ThisWorkbook.Sheets("Dashboard")
- For h = 1 To 3
- httpRequest.setRequestHeader .Range("rngAPIHeaderSR0" & CStr(h)).Offset(0, -1).Value, _
- .Range("rngAPIHeaderSR0" & CStr(h)).Value
- ThisWorkbook.Sheets("APICalls").Range("APICallSignatureRequest").Offset(h, 0) = .Range("rngAPIHeaderSR0" & CStr(h)).Offset(0, -1).Value
- ThisWorkbook.Sheets("APICalls").Range("APICallSignatureRequest").Offset(h, 1) = .Range("rngAPIHeaderSR0" & CStr(h)).Value
- Next h
- End With
- ' send full request
- With ThisWorkbook.Sheets("APICalls")
- .Range("APICallSignatureRequest01").Value = requestBodyPre
- .Range("APICallSignatureRequest02").Value = bytTempAll
- .Range("APICallSignatureRequest03").Value = requestBodyPost
- End With
- ' record response
- With ThisWorkbook.Sheets("Dashboard")
- .Range("rngResponseSigRequestStatus").Value = httpRequest.Status & "-" & httpRequest.statusText
- .Range("rngResponseSigRequestFull").Value = httpRequest.responseText
- End With
- MsgBox "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement