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!
CurrentBooksyou are corectly usingSet dicModels = New Dictionary. The code as it is here in the question should work, or at least thedicModelsshould not beNothing. Colud it be that something happens which we don't see here and which sets the dic. toNothing? And one question: the code ofUserForm_Initializebelongs to theFPowerPointright?UserForm_Initializebelongs toFPowerPoint. Clearly, something is setting the dic object toNothing. Otherwise, something is wrong with my scope. I'll do more debugging.