After Import of a Userform into VBComponents Properties cannot be read

前端 未结 1 359
别那么骄傲
别那么骄傲 2021-01-18 15:10

I try to import a Userform with:

With ownVBProj.VBComponents.Import(FileName:=FName)
  Print #2, FName; \" has \", .Properties.Count; \" Properties\"
End Wit         


        
相关标签:
1条回答
  • 2021-01-18 15:43

    It is not actually an answer to the problem, but some observation & and may be vague conclusion. I modified the test code and placed the Debug Print … statement after With .Import block with a vague idea to ‘Let the VBE 1st finish the import and let it be within the VBComponents collection before querying its property’. And It worked.

    Sub test()
        Dim FName As String
        With ThisWorkbook.VBProject.VBComponents ' save UserForm1
        Debug.Print "UserForm1 has " & .Item("UserForm1").Properties.Count & " properties before"
            With .Item("UserForm1")
            'Debug.Print "UserForm1 has " & .Properties.Count & " properties"
                FName = Environ$("Temp") & "\" & .Name & ".frm"
                If (LenB(Dir(FName)) <> 0) Then
                    Kill FName
                End If
                .Export Filename:=FName ' rename Form
                .Name = .Name & "_org"
            End With ' import
            With .Import(FName)
            'Debug.Print FName & " has " & .Properties.Count & " properties"
            End With
            Debug.Print "Userform_org has " & .Item("UserForm1_org").Properties.Count & " properties"
        End With
    End Sub
    

    I also have another observation. I also tried to count userform1’s property before exporting. I found it is working if only when the Userform1 properties window is active at the instance of running the code. Otherwise the same Error creeps in. I am using Excel 2007. May be it is some kind of bug in VBE.

    Edit2: wrt 2nd part of my problem, I further observed using . Activate for a particular VBComponent item prevent the Error. And its properties could be easily accessed. Trying the simple loop below both with and without Vbc.Activate line in any excel file containing multiple userForms and modules can demonstrate it.

    Dim Vbc As VBComponent
        For Each Vbc In ThisWorkbook.VBProject.VBComponents
        If Vbc.Type <> 100 Then   ‘ to exclude worksheets
        Vbc.Activate                        ‘  Try with or Without this line 
        Debug.Print Vbc.Name & " has " & Vbc.Properties.Count & " Properties"
        End If
        Next Vbc     
    
    0 讨论(0)
提交回复
热议问题