codingninja
codingninja

Reputation: 11

declaring and using a overall dictionary in vba

im new to programming and especially new to vba, but i'm trying my best and now i need your help!

So what i would like to achive:

I made a interface in word. There are multiple buttons. If one button is clicked, the programm should check if the button caption, which was clicked, is a key in a dictionary which is declared outside of the sub for the button click.

like :

i click on the button1 which states: "Work" -> check if "Work" is a key in dictionary -> if so, overwrite the button caption with the first item of the key "Work"

i click on the button2 which states: "home" -> check if "home" is a key in dictionary -> if so, overwrite the button caption with the first item of the key "home"

if there is no key to the buttons caption, it shall search through the document for the button caption and print the next paragraph in a label (-> this works quite well)

my approach:

  1. I tried to declare the dictionary (in main())

  2. I filled the dictionary(in main())

  3. I created the sub for the button click (a sub)

  4. optional: I created a function that is called to check if the button caption is a key in the dictionary (a function)

  5. if the return of the check is true: overwrite the button caption (in sub)

Problem:

so far my biggest problem is to make the dictionary accessibly by the button click subs. I just dont know how to properly declare it.

I tried to declare it as public like this:

Function Main()

    Public d As Scripting.Dictionary

    Set d = CreateObject("Scripting.Dictionary")
    d.Add "menu", Array("home", "friends", "money")
    d.Add "home", Array("cat", "dog", "mouse")
    d.Add "work", Array("boss", "employes")

End Function



Sub btn_Click()
    
    schl = btn.Caption
    If d.Exists(schl) = True Then
        x=d(schl)
        btn.Caption = x(1)
    End If
    
End Sub

-> Object required (Error 424)

I tried to declare d as Static like this (the rest is the same code):

Static d As Scripting.Dictionary 

-> compiling failure: User-defined type not defined

So i am very unsure about my concept and massively thankful for any hints on how to improve my style of coding and the programm itself! Thanks in advance!


EDIT: I tried the approach of Tim Williams:

Public d As Scripting.Dictionary

Function Main()
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "menu", Array("home", "friends", "money")
    d.Add "home", Array("cat", "dog", "mouse")
    d.Add "work", Array("boss", "employes")
End Function

and got: Object variable not set (Error 91)

if i understand BigBen and Manpreet Singh Dhillon correctely and tried their approach:

Public d 
Function Main()
    Set d = New Scripting.Dictionary 
    d.Add "menu", Array("home", "friends", "money") 
    d.Add "home", Array("cat", "dog", "mouse") 
    d.Add "work", Array("boss", "employes") 
End Function 

I get : Object required (Error 424)

Upvotes: 1

Views: 94

Answers (1)

Tim Williams
Tim Williams

Reputation: 166181

You need to declare the dictionary outside of the sub, as a Global variable (in addition to adding the scripting runtime reference to your project)

Public d As Scripting.Dictionary

Function Main()
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "menu", Array("home", "friends", "money")
    d.Add "home", Array("cat", "dog", "mouse")
    d.Add "work", Array("boss", "employes")
End Function

Upvotes: 1

Related Questions