1

I declared a public global dictionary object in a regular module as follows:

Public dicModels As Scripting.Dictionary  'Microsoft Scripting Runtime has been added as a reference

I have the following Callback:

Sub CreatePPT_OnAction(control As IRibbonControl)
    Call CurrentBooks
    Dim frmPPT_Slide As FPowerPoint
    Set frmPPT_Slide = New FPowerPoint
    frmPPT_Slide.Show
    Set frmPPT_Slide = Nothing
End Sub

Here is my subroutine for the call procedure:

Sub CurrentBooks()
    Dim wks As Excel.Worksheet
    Dim vObject As Variant

    If Not dicModels Is Nothing Then Exit Sub

    Set dicModels = New Dictionary
    For Each wks In ActiveWorkbook.Worksheets
        For Each vObject In wks.ListObjects
            If Left(vObject.Name, 3) = "TM_" Then
            dicModels.Add Key:=vObject.Name, Item:=Right(vObject.Name, Len(vObject.Name) - InStr(1, vObject.Name, "_"))
            End If
        Next vObject
    Next wks

End Sub

Here is my initialize Event in the Userform (iCounter is a module level variable declared as private):

Private Sub UserForm_Initialize()
    Me.Caption = "Main Tracking Model"
    Me.lblModel.Caption = "Choose a model to be reflected on the PPT slide."
    For iCounter = 0 To dicModels.Count  '<< ERROR!!!!!
        Me.lstModels.AddItem dicModels.Items(iCounter)
    Next iCounter
End Sub

I am trying to create a global dictionary object to be accessible from the userform class. Although I have it declared public at the module level I still get Object variable or With block variable not set. I must be misunderstanding or overlooking something. Any help is appreciated. Thanks!

2
  • In CurrentBooks you are corectly using Set dicModels = New Dictionary. The code as it is here in the question should work, or at least the dicModels should not be Nothing. Colud it be that something happens which we don't see here and which sets the dic. to Nothing? And one question: the code of UserForm_Initialize belongs to the FPowerPoint right? Commented Feb 23, 2017 at 13:08
  • @dee Yes, the code in UserForm_Initialize belongs to FPowerPoint. Clearly, something is setting the dic object to Nothing. Otherwise, something is wrong with my scope. I'll do more debugging. Commented Feb 23, 2017 at 13:14

1 Answer 1

1

Write as follows:

Public dicModels As New Scripting.Dictionary

This both declares the variable, and initializes it to a new Dictionary.


This initialization can be done together with the declaration. If the initialization is more complex, then you're probably better off not declaring the variable public, but rather have a public function that returns the value of the variable, and carries out any initialization if needed:

Dim m_dicModels As Scripting.Dictionary

Public Function dicModels() As Scripting.Dictionary
    If m_dicModels Is Nothing Then
        Set m_dicModels = New Scripting.Dictionary
        m_dicModels.CompareMode = TextCompare
    End If
    Set dicModels = m_dicModels
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the solution but I can't get it to work. When the userform is initialized I can't access the count property of the dictionary object. The dictionary is not nothing. Therefore, why can't I access any of its members from the userform initialize event?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.