尝试在 VBA 中发出 HTTP 请求时出现“没有合适对象的方法无效”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19553476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "method not valid without suitable object" error when trying to make a HTTP request in VBA?
提问by Chloe
I tried to follow this example: http://libkod.info/officexml-CHP-9-SECT-5.shtml- Archive.org- Donate
我试图按照这个例子:http: //libkod.info/officexml-CHP-9-SECT-5.shtml- Archive.org- Donate
but it gave this error
但它给出了这个错误
on this line:
在这一行:
Dim objHTTP As New MSXML2.XMLHTTP
I tried to use this example: How can I send an HTTP POST request to a server from Excel using VBA?
我尝试使用此示例:如何使用 VBA 从 Excel 向服务器发送 HTTP POST 请求?
but it gave this error:
但它给出了这个错误:
on this line:
在这一行:
Print objHTTP.Status
So how do I make a POST REST call in VBA? How do I make a PUT multi-part/form-data file upload REST call in VBA?
那么如何在 VBA 中进行 POST REST 调用呢?如何在 VBA 中进行 PUT 多部分/表单数据文件上传 REST 调用?
Tools > References
工具 > 参考
Code
代码
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"":""[email protected]"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
Print objHTTP.Status
Print objHTTP.ResponseText
End Sub
Reference
参考
WinHttpRequest object: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx
WinHttpRequest 对象:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85) .aspx
采纳答案by Monty Wild
You probably haven't added a reference to Microsoft XML
(any version) for Dim objHTTP As New MSXML2.XMLHTTP
in the VBA window's Tools/References... dialog.
您可能没有在 VBA 窗口的工具/引用...对话框中添加对Microsoft XML
(任何版本)Dim objHTTP As New MSXML2.XMLHTTP
的引用。
Also, it's a good idea to avoid using late binding (CreateObject
...); better to use early binding (Dim objHTTP As New MSXML2.XMLHTTP
), as early binding allows you to use Intellisense to list the members and do all sorts of design-time validation.
此外,最好避免使用后期绑定 ( CreateObject
...);最好使用早期绑定 ( Dim objHTTP As New MSXML2.XMLHTTP
),因为早期绑定允许您使用 Intellisense 列出成员并进行各种设计时验证。
回答by Chloe
I had to use Debug.print
instead of Print
, which works in the Immediate window.
我不得不使用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"":""[email protected]"",""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
回答by przemo_li
Check out this one:
看看这个:
https://github.com/VBA-tools/VBA-Web
https://github.com/VBA-tools/VBA-Web
It's a high level library for dealing with REST. It's OOP, works with JSON, but also works with any other format.
它是一个用于处理 REST 的高级库。它是 OOP,适用于 JSON,但也适用于任何其他格式。
回答by Justin Dearing
For reading REST data, at least OData Consider Microsoft Power Query. You won't be able to write data. However, you can read data very well.
对于读取 REST 数据,至少 OData 考虑 Microsoft Power Query。您将无法写入数据。但是,您可以很好地读取数据。