从 Word 使用 VBA 发送 HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3119207/
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
Sending HTTP requests with VBA from Word
提问by Saul
I am trying to send data from a Word document to a web page. I have found some code, pasted it into a new module, and saved it. When I run it I get "compile error, user defined type not defined"
我正在尝试将数据从 Word 文档发送到网页。我找到了一些代码,将其粘贴到一个新模块中并保存。当我运行它时,我得到“编译错误,未定义用户定义的类型”
My code:
我的代码:
Sub http()
Dim MyRequest As New WinHttpRequest
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
回答by Crispy
A potential alternative to avoid having to select the library is to use an object i.e.
避免必须选择库的潜在替代方法是使用对象,即
Sub http()
Dim MyRequest As Object
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
回答by Todd Main
You need to set a reference to Microsoft WinHTTP Services in your VBA Project (Tools -> References).
您需要在 VBA 项目(工具 -> 参考)中设置对 Microsoft WinHTTP 服务的引用。
Here's what it would look like:
这是它的样子:
Also, you can read more about the Microsoft WinHTTP Services, version 5.1 here.
此外,您可以在此处阅读有关 Microsoft WinHTTP 服务 5.1 版的更多信息。
回答by Fionnuala
You will need to change your references (Tools=>References in the code window). Look for Microsoft WinHTTP Services, version 5.1(or newer) and tick the box. If you are using Vista and office 2007, you may also need to register it first. Open a command window as administrartor and paste:
您将需要更改您的引用(代码窗口中的工具=> 引用)。寻找Microsoft WinHTTP Services, version 5.1(或更新)并勾选框。如果您使用的是 Vista 和 office 2007,您可能还需要先注册。以管理员身份打开命令窗口并粘贴:
>regsvr32.exe "c:\windows\system32\winhttp.dll"
It should say if it works.
它应该说它是否有效。

