VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.8k stars 574 forks source link

Compile Error: Invalid Use of New Keyword (Using Word 2016) #68

Open HighTechForms opened 7 years ago

HighTechForms commented 7 years ago
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

It's crashing on the line where it sets json_ParseObject.

StephaneSM commented 7 years ago

Hello,

There is an incompatibility with the dictionary object of Word. Insert "scripting." before the term dictionary. Example:

Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As scripting.Dictionary
    Dim json_Key As String
    Dim json_NextChar As String

    Set json_ParseObject = New scripting.Dictionary
timhall commented 6 years ago

@StephaneSM thanks for responding. I'll have to investigate why this only occurs in Word, I'll see if there's a good way to fix this at the library level (ideally you wouldn't need to edit anything to get it to work out-of-the-box in Word)

AimForNaN commented 6 years ago

I can confirm this is not limited to Word, but I did manage to narrow down the problem and have a solution. You must make sure that the "Microsoft Scripting Runtime" comes before any of the Microsoft Word references. In other words, the scripting runtime reference must have higher priority in the References list than the other Microsoft references.

I would recommend that the readme be updated to inform users that might make use of any conflicting references.

skatun commented 6 years ago

I can confirm the same issues, changing to scripting.Dictionary helped

shula commented 6 years ago

Maintainers: Please change the module to use "New Scripting.Dictionary" anyway.

The full syntax is compatible both with Word and with Excel, whereas "new Dictionary" is compatible only with Excel.

Or at least write about it in the Readme.md, just next to the "add reference to MS Scripting", e.g. "In order to run it in MS-Word, replace any "New Dictionary" with "New scripting.Dictionary". Excel developers should run it as is".

MoSad85 commented 6 years ago

I confirm the issue scripting.Dictionary not really helped. the error occure on this line image I m using the lib with Word in Office 360. Error on compile: Method or data object not found.

MoSad85 commented 6 years ago

Hi, i found a solution thats works for me in word. Just edit the function 'json_Object'. Create the dictionary at runtime and return an object instead of Dictionary. Like This: image

philg74 commented 3 years ago

Hello,

There is an incompatibility with the dictionary object of Word. Insert "scripting." before the term dictionary. Example:

Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As scripting.Dictionary
    Dim json_Key As String
    Dim json_NextChar As String

    Set json_ParseObject = New scripting.Dictionary

Hello, In order to avoid error "Method or data object not found" at json_ParseObject.Item(json_Key), dont forget to add "Scripting." in both:

Deedolith commented 2 years ago

Has anyone made a pull request on this ? I strongly suggest MoSad85's solution, in other words switch the library to late binding, since early binding is a known source of troubles.

Deedolith commented 2 years ago

Well, a pull request is done, and up for review.

AnywhereWithyou commented 1 year ago

I think that my solution is the best for above error. https://github.com/VBA-tools/VBA-JSON I didn't face any error when I used above jsonconverter in excel vba for userform. But I faced same error in excel vba project for form control. MoSad85's solution is not working to me.

Screenshot_1

Solution: 1.Remove MS script runtime library from reference in excel vba 2.Import above cls to vba project.

That's all