VBA中的HTTP响应(GET)

时间:2019-02-04 08:35:30

标签: vba outlook

如何使用VBA GET进行响应?

此代码无效。在Debug.Pring()MsgBox中为空。

TargetURL = snURL + selectedMail
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Open "GET", TargetURL, False
HTTPReq.SetCredentials snUser, snPass, 0
HTTPReq.setRequestHeader "Accept", "application/json"

Debug.Print HTTPReq.responseText

我想获取JSON数据。

1 个答案:

答案 0 :(得分:0)

可选库(如果使用早期绑定,则需要此库,通常在没有它们的情况下代码也可以工作):

enter image description here

更改companyName变量:

Sub TestMe()

    Dim xmlObject As Object
    Dim companyName As String: companyName = "Google"
    Dim strUrl As String
    strUrl = "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=" & companyName

    Set xmlObject = CreateObject("MSXML2.XMLHTTP")
        With xmlObject
        .Open "GET", strUrl, False
        .Send
    End With

    Dim response As String
    response = "{""data"":" & xmlObject.ResponseText & "}"
    Debug.Print response

End Sub