Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Import of a Userform into VBComponents Properties cannot be read

I try to import a Userform with:

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

During execution I get the error

-2147467259 (80004005) raises (method 'properties' for the object '_VBComponent' has failed)

Although the userform has correctly been imported - I can see it in the formula window.

If I inspect the newly imported component with the object inspector, I can see the property tree, and after I have done this the code can be continued!! Weird.

Does someone has any suggestions to avoid the problem?

Edit:

Here is a complete example:

  • create a new excel sheet
  • insert a userform
  • 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 
like image 700
Burghart Hoffrichter Avatar asked Apr 30 '17 06:04

Burghart Hoffrichter


1 Answers

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     
like image 177
Ahmed AU Avatar answered Oct 21 '22 16:10

Ahmed AU