After importing a custom form into VBComponents properties, you cannot read

I am trying to import Userform with:

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

At runtime, I get an error

-2147467259 (80004005) raises (method properties for object "_VBComponent" failed)

Although the user form is correctly imported, I see it in the formula window.

If I inspect a recently imported component with an object inspector, I can see the property tree, and after I do this, the code can continue! Weird

Does anyone have any suggestions for fixing the problem?

Edit:

Here is a complete example:

  • create a new excel sheet
  • insert user form
  • execute the following code:

Sub test()
    Dim FName As String
    With ThisWorkbook.VBProject.VBComponents ' save UserForm1
        With .Item("UserForm1")
            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
    End With
End Sub 
+6
1

, . Debug Print … With.Import : " VBE 1st VBComponents, . .

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

. userform1s . , , Userform1 . . Excel 2007. , - VBE.

Edit2: , . Activate . Activate VBComponent, . . Vbc.Activate , , Vbc.Activate excel, userForms , .

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     
+2

Source: https://habr.com/ru/post/1676016/


All Articles