'Undefined object variable' error using scriptForge dictionary

Hello everyone,

I have a trouble to run one of my macro while using a dictionary. Here is how I initialize it (Sorry some of my variables and comments are in french) :

Global dictGlobal As Variant
'Global oFinishedDial As Object
Global iRow As Integer
Global Const Tolerance As Double = 0.00 'seuil de tolérance pour écart

’ Initialisation du dictionnaire global pour stocker les numéros de compte et leurs feuilles
Sub InitializeDictionaries()
’ Appel de la librairie ScriptForge
GlobalScope.BasicLibraries.LoadLibrary(“ScriptForge”)
dictGlobal = CreateScriptService(“Dictionary”)
MsgBox “Dictionnaire initialisé”
End Sub

’ Fonction pour vérifier et initialiser le dictionnaire si nécessaire
Function EnsureDictionaryInitialized() As Boolean
If IsNull(dictGlobal) Or IsEmpty(dictGlobal) Then
InitializeDictionaries()
EnsureDictionaryInitialized = Not (IsNull(dictGlobal) Or IsEmpty(dictGlobal))
Else
EnsureDictionaryInitialized = True
End If
End Function

Then I call the sub inside one of my sub :

Sub CompareAndCreateSheets

' Vérifiez si le dictionnaire est initialisé
If Not EnsureDictionaryInitialized() Then
    MsgBox "Le dictionnaire global n'a pas pu être initialisé.", 48, "Erreur"
    Exit Sub
End If

…(Then more codes in the middle…)

’ I Have an exception here while i try to verifie if the element is inside the dictionary

    If Not bFound Then
       WriteRowToSheet(oSheet1, aOrdonnateurData(i), iRowOrdoDiff)
       'Ajouter au dictionnaire 
       Dim NumCompteAbsentString As String
       Dim NumCompteAbsent As String
       NumCompteAbsentString = CStr(aOrdonnateurData(i)(iOrdNumInvent))
       NumCompteAbsent = Cstr(CleanStringValue(NumCompteAbsentString))
	   
       If Not dictGlobal.Exists(NumCompteAbsent) Then
           dictGlobal.Add(NumCompteAbsent, CreateScriptService("Dictionary"))
       End If
       If Not dictGlobal(NumCompteAbsent).Exists("OrdNumInvent_Absent") Then
           dictGlobal(NumCompteAbsent).Add("OrdNumInvent_Absent", Array())
       End If
       Dim ordArray As Variant
       ordArray = dictGlobal(NumCompteAbsent)("OrdNumInvent_Absent")
       ordArray = AddToArray(ordArray, aOrdonnateurData(i))
       dictGlobal(NumCompteAbsent)("OrdNumInvent_Absent") = ordArray
    End If
Next i

If Someone can help me solve it . Id be Grateful . Have a good Sunday

Would uploading the file be an option? It might get things looked at more quickly.

here is the code . its long and in french sometimes.
MacroCompaEtatActifAvecEcartV4.ods (104.3 KB)

Thanks for that.

I have had
BreakingDictGlobal.ods (11.2 KB)
very little time to glance at this. However, is dictGlobal being converted into an array instead of an Object in the lines dealing with ordArray?

I’ve separated out just the salient bits in the attached file. If you execute Test2 once it works, but then the next time it fails. However, looking at dictGlobal through XRay, the first time it seems like an object as expected. The next time it appears to be ordArray itself. Is it a question of expectations about assign-by-value vs. assign-by-reference?

Thanks for taking the time to check my macro. I did notice that too : If I execute once it works and the next iteration there is an error. Maybe be I’m doing this bad but the goal was to create a dictionary :dictGlobal which contains as keys NumCompteAbsent and value another dictionary. The sub-dictionnary got as keys for example “OrdNumIvent_Absent” and value an array like “ordArray”. I really don’t know how to assign those values so that dictGlobal doesn’t get converted into an array