设置对象时键入不匹配

时间:2017-06-26 14:01:43

标签: vba

我正在尝试使用VBa查询Web API。

我遇到的问题是返回结果会抛出此异常

  

类型不匹配

当我退出getJson函数(如下所示)

时会发生这种情况
Function StartOfCode()
'...code
Dim jsonResult As Object
Set jsonResults = getJson(query)   'cannot get past this
'... more code
End Function


Function getJson(ByRef query As String) As Object

Dim MyRequest As Object
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")

    With MyRequest
        .Open "GET", query
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .send           
    End With

Set getJson = DecodeJson(MyRequest.ResponseText) ' returns fine and I can see the object, of type Object/JScript/TypeInfo

Set MyRequest = Nothing

End Function

Function DecodeJson(JsonString As Variant) As Object
    Set DecodeJson = m_ScriptEngine.Eval("(" + JsonString + ")")   
End Function

我不明白我在这里做错了什么

1 个答案:

答案 0 :(得分:1)

问题在于对象声明:

Dim jsonResult As Object Set jsonResults = getJson(query)

您已声明了一个名为jsonResult的对象,但在下一行中您使用的是varibable名称的复数形式:jsonResults。因此,您需要更改其中一个变量名称,以便它们匹配。