Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. ' reset request and result vars
  2. Set httpRequest = New XMLHTTP60
  3.  
  4. With ThisWorkbook.Sheets("Dashboard")
  5. ' clear the cells holding the response
  6. .Range("rngResponseSigRequestStatus").Value = vbNullString
  7. .Range("rngResponseSigRequestFull").Value = vbNullString
  8.  
  9. ' define the new URL for the signature request using the Base URL returned aboved appended with '/envelopes'
  10. URL = .Range("rngResponseLoginBaseUrl").Value & "/envelopes"
  11. End With
  12.  
  13. ' start the body of the request
  14. requestBody = "<envelopeDefinition xmlns=" & Chr(34) & "http://www.docusign.com/restapi" & Chr(34) & ">"
  15.  
  16. ' add email info to the body
  17. requestBody = requestBody & "<emailSubject>API Call for adding signature request to document and sending</emailSubject>" & _
  18. "<status>sent</status>"
  19.  
  20. ' add documents to the body
  21. requestBody = requestBody & "<documents>" & _
  22. "<document>" & _
  23. "<documentId>1</documentId>" & _
  24. "<name>" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value & "</name>" & _
  25. "</document>" & _
  26. "</documents>"
  27.  
  28. ' add email recipients
  29. requestBody = requestBody & "<recipients>" & _
  30. "<signers>" & _
  31. "<signer>" & _
  32. "<recipientId>1</recipientId>" & _
  33. "<name>" & ThisWorkbook.Sheets("Dashboard").Range("rngRecipientName").Value & "</name>" & _
  34. "<email>" & ThisWorkbook.Sheets("Dashboard").Range("rngRecipientEmail").Value & "</email>" & _
  35. "<tabs>" & _
  36. "<signHereTabs>" & _
  37. "<signHere>" & _
  38. "<xPosition>100</xPosition>" & _
  39. "<yPosition>100</yPosition>" & _
  40. "<documentId>1</documentId>" & _
  41. "<pageNumber>1</pageNumber>" & _
  42. "</signHere>" & _
  43. "</signHereTabs>" & _
  44. "</tabs>" & _
  45. "</signer>" & _
  46. "</signers>" & _
  47. "</recipients>"
  48.  
  49. ' close the envelope definition
  50. requestBody = requestBody & "</envelopeDefinition>"
  51.  
  52. ' read in document in byte array
  53. Dim intFileNum%, bytTemp As Byte, intCellRow
  54. Dim bytTempAll As String
  55. intFileNum = FreeFile
  56. intCellRow = 0
  57. Open (ThisWorkbook.Path & "" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value) For Binary Access Read As intFileNum
  58. Do While Not EOF(intFileNum)
  59. ' intCellRow = intCellRow + 1
  60. Get intFileNum, , bytTemp
  61. 'Cells(intCellRow, 1) = bytTemp
  62. bytTempAll = bytTempAll & bytTemp
  63. Loop
  64. Close intFileNum
  65. Dim lenFile As Long
  66. lenFile = Len(bytTempAll) ' just curious
  67.  
  68. ' read document in adobe stream
  69. Set AdobeStream = CreateObject("ADODB.STREAM")
  70. With AdobeStream
  71. .Type = 1 ' binary
  72. .Open ' open the file
  73. .LoadFromFile (ThisWorkbook.Path & "" & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value)
  74. End With
  75.  
  76. ' create multipart/form-data request
  77. Dim requestBodyPre As String
  78. requestBodyPre = vbCrLf & vbCrLf & "--BOUNDARY" & _
  79. vbCrLf & "Content-Type: application/xml" & _
  80. vbCrLf & "Content-Disposition: form-data" & vbCrLf & _
  81. vbCrLf & requestBody & _
  82. vbCrLf & vbCrLf & "--BOUNDARY" & _
  83. vbCrLf & "Content-Type: " & ThisWorkbook.Sheets("Dashboard").Range("rngContentType").Value & _
  84. vbCrLf & "Content-Disposition: file; filename=" & Chr(34) & ThisWorkbook.Sheets("Dashboard").Range("rngDocumentName").Value & Chr(34) & "; documentid=1" & _
  85. vbCrLf
  86.  
  87. ' break up the requestBody into 'Pre' and 'Post' because document bytes need to be passed in the middle
  88. Dim requestBodyPost As String
  89. requestBodyPost = vbCrLf & "--BOUNDARY--" & vbCrLf & vbCrLf
  90.  
  91. ' open url
  92. Debug.Print "POSTing URL " & URL
  93. httpRequest.Open "POST", URL, False
  94. ThisWorkbook.Sheets("APICalls").Range("APICallSignatureREquest").Value = URL
  95.  
  96.  
  97. ' attach headers
  98. With ThisWorkbook.Sheets("Dashboard")
  99. For h = 1 To 3
  100. httpRequest.setRequestHeader .Range("rngAPIHeaderSR0" & CStr(h)).Offset(0, -1).Value, _
  101. .Range("rngAPIHeaderSR0" & CStr(h)).Value
  102. ThisWorkbook.Sheets("APICalls").Range("APICallSignatureRequest").Offset(h, 0) = .Range("rngAPIHeaderSR0" & CStr(h)).Offset(0, -1).Value
  103. ThisWorkbook.Sheets("APICalls").Range("APICallSignatureRequest").Offset(h, 1) = .Range("rngAPIHeaderSR0" & CStr(h)).Value
  104. Next h
  105. End With
  106.  
  107. ' send full request
  108. With ThisWorkbook.Sheets("APICalls")
  109. .Range("APICallSignatureRequest01").Value = requestBodyPre
  110. .Range("APICallSignatureRequest02").Value = bytTempAll
  111. .Range("APICallSignatureRequest03").Value = requestBodyPost
  112. End With
  113.  
  114. ' record response
  115. With ThisWorkbook.Sheets("Dashboard")
  116. .Range("rngResponseSigRequestStatus").Value = httpRequest.Status & "-" & httpRequest.statusText
  117. .Range("rngResponseSigRequestFull").Value = httpRequest.responseText
  118. End With
  119.  
  120. MsgBox "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement