VBA-tools / VBA-Dictionary

Drop-in replacement for Scripting.Dictionary on Mac
MIT License
348 stars 89 forks source link

Exception with key declared as string and modified in sub byref as variant #27

Open omegastripes opened 4 years ago

omegastripes commented 4 years ago

There is a simplified snippet from a project with Scripting.Dictionary, which works fine:

Sub testDict()

    Dim dict As Variant
    addValue dict

End Sub

Sub addValue(dict)

    Dim key As String

    Set dict = CreateObject("Scripting.Dictionary")
    createKey key
    dict(key) = "value"

End Sub

Sub createKey(key)

    key = "a"

End Sub

There is the same snippet with VBA-Dictionary, and it throws an exception in Property Let Item(): "Invalid procedure call or argument" with #Const UseScriptingDictionaryIfAvailable = True or "Variable uses an Automation type not supported in Visual Basic" with #Const UseScriptingDictionaryIfAvailable = False

Sub testDict()

    Dim dict As Variant
    addValue dict

End Sub

Sub addValue(dict)

    Dim key As String

    Set dict = New Dictionary
    createKey key
    dict(key) = "value"

End Sub

Sub createKey(key)

    key = "a"

End Sub

Note for commenters: I do not need to change anything in the project, more of that, the issue can be fixed by Dim key As Variant. The snippet is simplified as much as possible, also subs are used instead of functions because they returns data via several variables byref in the project. It works with no issues with Scripting.Dictionary, now I tried VBA-Dictionary and found some difference, that is why I reported here.