Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a global Scripting.Dictionary in Visual Basic for Applications (VBA)

Tags:

dictionary

vba

I have a function that is appending a dictionary. I want to keep the contents of this dictionary as long as the updateList isn't true. I currently have it setup like this:

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)

Dim dicTitle As Variable
Dim dicFound As Variable

If updateList = True Then
Set dicTitle = CreateObject("Scripting.Dictionary")
Set dicFound = CreateObject("Scripting.Dictionary")

While status
        Set ObjectsJSON = jsonlibPollux.parse(responseString)

        With dicTitle
        .Add inc, ObjectsJSON.Item("title")
        End With

        With dicFound
        .Add inc, ObjectsJSON.Item("description")
        End With

Wend
End If

And when voerUit is true the following happens:

For i = 1 To dicTitle.Count
klaar = findReplace(dicTitle.Item(i), "x" + dicTitle.Item(i), dicTitle.Item(i) + vbCrLf + vbCrLf + dicFound.Item(i))
Next i

The problem here is that when this function ends, dicTitle and dicFound are cleared, and the findReplace function gets fed empty arguments.

Is there anyway to makes to code work, or a good workaround?

like image 559
Martijn Nosyncerror Avatar asked Mar 13 '14 15:03

Martijn Nosyncerror


People also ask

How do I add a dictionary to VBA?

Basic setup of a VBA Dictionary For early binding, start by opening the Visual Basic Editor by pressing Alt+F11 and going to Tools > References. Set a reference to the Microsoft Scripting Runtime object library.

What is the difference between collection and dictionary in VBA?

You can assign a Key to an Item when you add the Item to the Collection, but you cannot retrieve the Key associated with an Item nor can you determine (directly) whether a key exists in a Collection. Dictionaries are much friendly and open with their keys. Dictionaries are also considerably faster than Collections.

How to use VBA dictionary in Visual Studio Code?

To use VBA Dictionary, we need to enable Microsoft Scripting Runtime function which is available in Tool menu option under References. Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more. VBA Dictionary has a few major properties as explained below; Count = It returns the count of a number of variable in the dictionary key.

How do I create a dictionary using Microsoft scripting runtime?

Find Microsoft Scripting Runtime in the list and place a check in the box beside it. Creating a Dictionary in this way is called “Early Binding”. There is also “Late Binding”. Let’s have a look at what this means.

How do I use a dictionary?

This is a simple example but it shows how useful a Dictionary is. We will see a real world example later in the post. Let’s look at the basics of using a Dictionary. To use the Dictionary you need to first add the reference. Select Tools->References from the Visual Basic menu.

What are the characteristics of a VBA Dictionary?

As in a normal word dictionary, we see words with their meaning, categories, application, pronunciation, etc. In VBA Dictionary as well we can define all the necessary characteristics and value which we want to assign to any word or variable.


1 Answers

You will need to add a reference to the Microsoft Scripting Runtime to use binding like I have used. It is preferable to the manner you are declaring them as you have better access to the object and can see member variables easier, etc.

Method 1 - module level or public variables for dictionaries

One way you can do this is to create module level variables for the scripting dictionaries you are using:

Public gDicTitle As Scripting.Dictionary
Public gDicFound As Scripting.Dictionary

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)


    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary
        
        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)
        
                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With
        
                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With
    
        Wend
    End If
End Function

Method 2 - static references to dictionaries

You can also do this by making the dictionaries static. This will preserve them between function calls like the documentation says.

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
    Static gDicTitle As Scripting.Dictionary
    Static gDicFound As Scripting.Dictionary
    
    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary
        
        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)
        
                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With
        
                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With
    
        Wend
    End If
End Function
like image 101
enderland Avatar answered Sep 18 '22 19:09

enderland