将用户窗体导入VBComponents属性后,无法读取 [英] After Import of a Userform into VBComponents Properties cannot be read

查看:197
本文介绍了将用户窗体导入VBComponents属性后,无法读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下方式导入用户表单:

I try to import a Userform with:

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

在执行过程中出现错误

-2147467259(80004005)引发(对象"_VBComponent"的方法属性"失败)

-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?

这是一个完整的示例:

  • 创建一个新的Excel工作表
  • 插入用户表格
  • 执行以下代码:
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 

推荐答案

这实际上不是问题的答案,而是一些观察和发现.可能是含糊的结论.我修改了测试代码,并在With .Import块之后放置了Debug Print …语句,并带有一个模糊的想法:让VBE 1st完成导入,并在查询其属性之前将其放入VBComponents集合中".而且有效.

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

我还有另外一个发现.我还尝试在导出前计算userform1的属性.我发现只有在运行代码的实例中Userform1属性窗口处于活动状态时,它才起作用.否则,会出现相同的错误.我正在使用Excel2007.可能是VBE中的某种错误.

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 :问题的第二部分,我进一步观察到对于特定的VBComponent项目使用. Activate可以防止错误.而且它的属性可以很容易地访问.尝试在包含多个userForms和模块的任何excel文件中使用Vbc.Activate行和不使用Vbc.Activate行下面的简单循环都可以证明这一点.

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     

这篇关于将用户窗体导入VBComponents属性后,无法读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆