从Word发送带有VBA的HTTP请求

Sau*_*aul 20 vba ms-word httprequest

我正在尝试将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
Run Code Online (Sandbox Code Playgroud)

小智 26

避免必须选择库的潜在替代方案是使用对象即

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
Run Code Online (Sandbox Code Playgroud)


Tod*_*ain 21

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

这是它的样子:

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

  • @Saul:这通常意味着代码或调试器仍在运行.尝试单击Run-> Reset,然后单击Tools> References. (2认同)