我尝试按照此示例:http://libkod.info/officexml-CHP-9-SECT-5.shtml - Archive.org - Donate
但它给出了这个错误
在这一行:
Dim objHTTP As New MSXML2.XMLHTTP
我尝试使用此示例:How can I send an HTTP POST request to a server from Excel using VBA?
但它给出了这个错误:
在这一行:
Print objHTTP.Status
那么如何在VBA中进行POST REST调用?如何在VBA中进行PUT多部分/表单数据文件上载REST调用?
Sub SendEmail()
'Dim objHTTP As New MSXML2.XMLHTTP
'Set objhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://localhost:8888/rest/mail/send"
objHTTP.Open "POST", URL, False
objHTTP.send ("{""key"":null,""from"":""me@me.com"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
Print objHTTP.Status
Print objHTTP.ResponseText
End Sub
WinHttpRequest对象:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx
答案 0 :(得分:15)
您可能无法在VBA窗口的工具/参考文件...对话框中为Microsoft XML
添加Dim objHTTP As New MSXML2.XMLHTTP
(任何版本)的引用。
此外,避免使用后期绑定(CreateObject
...)是一个好主意;最好使用早期绑定(Dim objHTTP As New MSXML2.XMLHTTP
),因为早期绑定允许您使用Intellisense列出成员并进行各种设计时验证。
答案 1 :(得分:13)
我必须使用Debug.print
而不是Print
,这可以在立即窗口中使用。
Sub SendEmail()
'Dim objHTTP As New MSXML2.XMLHTTP
'Set objHTTP = New MSXML2.XMLHTTP60
'Dim objHTTP As New MSXML2.XMLHTTP60
Dim objHTTP As New WinHttp.WinHttpRequest
'Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
'Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://localhost:8888/rest/mail/send"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.send ("{""key"":null,""from"":""me@me.com"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
Debug.Print objHTTP.Status
Debug.Print objHTTP.ResponseText
End Sub
答案 2 :(得分:12)
答案 3 :(得分:0)
要读取REST数据,至少应考虑Microsoft Data Query。您将无法写入数据。但是,您可以很好地读取数据。