Function can’t return dictionary in VBA

I’m working on an EXCEL Macro (Microsoft office 365 for entreprise – can’t find the version number).
I’m struggling with a small problem, I would appreciate some help.

In my code (below), I work with the CAD softwareCATIA to modify some CATParts properties. All the updates I have to do are stored in an Excel worksheet named CATParts.

I split my code in two parts :

  • GetPartsExportData, a function to read all properties and store it in a dictionary organized as this : <Key=Part name> : <Value = Dict[<Key=property>:<Value=property value>]>. This function returns the dictionary
  • Test : The sub that gets the result of GetPartsExportData to do some process with CATIA. No matter what happens thgen, the problem is linked to this dictionary return.

When I run the Sub Test, I get the following error : Object required. I put a Debug.Print before return : ExportData type is Dictionary. I put a second Debug.Print after catching the return value of the function and there ExportData Type is Empty. So I understand the error but I can’t find a solution to solve this issue.

I made some researches and maybe it is a problem with Value/Object-Set/no Set. I’m a beginner with VBA so I’ve not yet really well understood how to manipulate objects.

Does someone have an explaination for this problem ?

The code :

Sub Test()
    Set catia = GetObject(, "CATIA.APPLICATION")
    
    ExportData = GetPartsExportData("Part3")
    Debug.Print ("Type after return : " & TypeName(ExportData))
    
    For i = 1 To catia.Documents.Count
        Application.StatusBar = i & "/" & catia.Documents.Count
        Set myDocument = catia.Documents.Item(i)
        
        If TypeName(myDocument) = "PartDocument" Then ' Ne traiter que les CATParts
            Set myProduct = myDocument.product
            Set myPart = catia.Documents.Item(i).part
        
            ' Vérifier que la Part a bien été détectée à l'import
            Flag = False
            For Each Name In PartsNameCollection
                If Name = myPart.Name Then
                    Flag = True
                End If
            Next Name
            If Flag = True Then
            Debug.Print TypeName(ExportData)
                Dim PartProperties As New Scripting.Dictionary
                For Each k In ExportData.Keys
                    Debug.Print "-->" & k & "-"
                Next k
                Debug.Print "-------------------------------------------"
                Debug.Print "Existe ?: " & ExportData.Exists(myPart.Name) & "-" & myPart.Name & "-" & ExportData.Keys(0) & "-"
                Debug.Print myPart.Name = ExportData.Keys(1) & "-" & TypeName(myPart.Name) & "-" & TypeName(ExportData.Keys(0))
                Debug.Print TypeName(ExportData(myProduct.PartNumber))
                
                For Each prop In PropertiesCatalogue
                    Flag = False
                    For Each Property In myProduct.UserRefProperties
                        tmp = Split(Property.Name, "")
                        UserPropertyName = tmp(UBound(tmp)) 'Nom de propriété raccourci
                        
                        If UserPropertyName = prop Then
                            Flag = True
                            Set myProduct.UserRefProperties.Item(UserPropertyName).Value = PartProperties(prop)
                            Exit For
                        End If
                    Next Property
                    If Flag = False Then
                        Debug.Print "*********************** " & ExportData(myProduct.PartNumber).Item(prop)
                        myProduct.UserRefProperties.CreateString prop, ExportData(myProduct.PartNumber).Item(prop)
                    End If
                Next prop
            End If
        End If
        
    Next i
End Sub

Public Function GetPartsExportData() As Scripting.Dictionary
    ' Renvoie un dictionnaire qui contient les propriétés modifiées/mises à jour à partir de l'onglet 'CATParts'
    ' Les données exportées ne sont pas liées aux données importées, le dictionnaire est recomposé à partir d'excel uniquement
    Dim ExportData As Scripting.Dictionary
    Set ExportData = New Scripting.Dictionary
    
    For i = 1 To PartsCollection.Count
        Dim PartsData As Scripting.Dictionary
        Set PartsData = New Scripting.Dictionary
        
        For j = 1 To PropertiesCatalogue.Count
            prop = PropertiesCatalogue(j)
            PartsData.Add prop, Worksheets("CATParts").Cells(i + 2, j) ' Décalage d'index à cause de la mise en page de l'onglet
        Next j
        ExportData.Add Worksheets("CATParts").Cells(i + 2, 1), PartsData
    Next i
    Debug.Print "Type before return : " & TypeName(ExportData)
    
    Set GetPartsExportData = ExportData
End Function

The Output :
Type before return : Dictionary
Type after return : Empty

The error : “424 : Objet requis”

I changed

  • Dim ExportData As Scripting.Dictionary into As New Scripting.Dictionary in the sub and the function
  • I tried to put and remove set before asignations

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật