Skip to content

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

@omegastripes

Description

@omegastripes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions