MyDaftQuestions
MyDaftQuestions

Reputation: 4701

Type Mismatch when setting an object

I am trying to query a web API using VBa.

The issue I am having is the return result throws this exception

Type Mismatch

This occurs when I exit the getJson function (shown below)

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

I don't understand what I've done wrong here

Upvotes: 0

Views: 235

Answers (1)

J. Garth
J. Garth

Reputation: 803

The problem is with the object declaration:

Dim jsonResult As Object Set jsonResults = getJson(query)

You have declared an object called jsonResult but in the next line you are using the plural form of the varibable name: jsonResults. So you need to change one of these variable names so they match.

Upvotes: 1

Related Questions