VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.74k stars 565 forks source link

JsonConverter.ParseJson() showing error "incorrect use of the New keyword" #237

Closed Zeyna-sarr closed 1 year ago

Zeyna-sarr commented 2 years ago

Hello everyone,

I'm testing web api call in VBA Word but I'm experiencing an issue trying to convert the result.

When I do : " Dim response As Object Set response = JsonConverter.ParseJson(request.ResponseText) "

and compile, it point out the JsonConverter file displaying the error "incorrect use of the New keyword".

Here is the concerned part in the JsonConverter file : " Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary Dim json_Key As String Dim json_NextChar As String

  Set json_ParseObject = New Dictionary (<------- The line error )

............ "

Any help will be appreciated

houghtonap commented 2 years ago

I suspect that you have not included a reference in your VBA project for Microsoft Scripting Runtime which is where you will find the object Scripting.Dictionary. The VBA-JSON source assumes that, that reference has been added to your VBA project.

  1. File->Options->Customize Ribbon, in the right pane (Customize the Ribbon) check the Developer tab to show it, if you have not already done so, then click the OK button.
  2. Click the Developer tab, then click the Visual Basic button on the ribbon.
  3. Click Tools->References, then scroll down the list until you see Microsoft Scripting Runtime and check it and click the OK button.

Hopefully that clears up the issue.

Zeyna-sarr commented 2 years ago

Hi @houghtonap, Thank you for your response but I've already checked Microsoft Scripting Runtime in the project. So that's not the problem I guess.

houghtonap commented 2 years ago

A few questions:

  1. What is request.ResponseText, I'm suspecting it's from a call to WinHTTP or XMLHTTP, so see my response to issue #221 and whether that is applicable. If it is, then I suspect the asynchronous response returned later and the value of request.ResponseText was Nothing and thus caused an issue in the VBA-JSON code. Use a synchronous request or a call back handler with an asynchronous request.
  2. You said you did:
    Dim response As Object
    Set response = JsonConverter.ParseJson(request.ResponseText)

    But it looks like you are expecting a JSON Object back, which in VBA-JSON is mapped to a Scripting.Dictionary object. So you should really be doing Dim response As Scripting.Dictionary however, As Object will work, but you will loose intellisense in the VBA editor.

Nick-vanGemeren commented 2 years ago

This is a duplicate of open issue #68 with one solution in pull request #219. If that solves your issue, please close it.

Zeyna-sarr commented 1 year ago

Great šŸ‘ the pull request #219 solves it for me. Thanks you

Zeyna-sarr commented 1 year ago

Hi, Thank you for your response but I've already checked Microsoft Scripting Runtime in the project. So that's not the problem I guess.

Le mer. 17 aoƻt 2022 Ơ 16:10, Andrew Houghton @.***> a Ʃcrit :

I suspect that you have not included a reference in your VBA project for Microsoft Scripting Runtime which is where you will find the object Scripting.Dictionary. The VBA-JSON source assumes that, that reference has been added to your VBA project.

  1. File->Options->Customize Ribbon, in the right pane (Customize the Ribbon) check the Developer tab to show it, if you have not already done so, then click the OK button.
  2. Click the Developer tab, then click the Visual Basic button on the ribbon.
  3. Click Tools->References, then scroll down the list until you see Microsoft Scripting Runtime and check it and click the OK button.

Hopefully that clears up the issue.

ā€” Reply to this email directly, view it on GitHub https://github.com/VBA-tools/VBA-JSON/issues/237#issuecomment-1218063865, or unsubscribe https://github.com/notifications/unsubscribe-auth/AS2BMWL7KTTCVSTV5RFHMKTVZTXGFANCNFSM56BGY2FQ . You are receiving this because you authored the thread.Message ID: @.***>