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
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 afterWith .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.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 withoutVbc.Activate
line in any excel file containing multiple userForms and modules can demonstrate it.