使用 VBA 从 Word 发送 HTTP 请求

发布于 2024-09-07 18:38:07 字数 341 浏览 4 评论 0原文

我正在尝试将 Word 文档中的数据发送到网页。我找到了一些代码,将其粘贴到新模块中并保存。当我运行它时,我收到“编译错误,用户定义的类型未定义”

我的代码:

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

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"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-09-14 18:38:07

避免选择库的潜在替代方法是使用对象,即

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

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
メ斷腸人バ 2024-09-14 18:38:07

您需要在 VBA 项目中设置对 Microsoft WinHTTP Services 的引用(工具 -> 引用)。

它看起来像这样:

另外,您可以阅读有关 Microsoft WinHTTP 的更多信息服务版本 5.1 此处

You need to set a reference to Microsoft WinHTTP Services in your VBA Project (Tools -> References).

Here's what it would look like:

Also, you can read more about the Microsoft WinHTTP Services, version 5.1 here.

笑,眼淚并存 2024-09-14 18:38:07

您将需要更改您的引用(代码窗口中的工具=>引用)。查找 Microsoft WinHTTP Services 版本 5.1(或更高版本)并勾选该框。如果您使用的是Vista和office 2007,您可能还需要先注册。以管理员身份打开命令窗口并粘贴:

>regsvr32.exe "c:\windows\system32\winhttp.dll"

它应该显示它是否有效。

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:

>regsvr32.exe "c:\windows\system32\winhttp.dll"

It should say if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文