简体   繁体   English

访问没有Form实例的VBA Userform控件

[英]Access VBA Userform Controls without Form instance

I have a friend with a VBA project in Excel. 我有一个朋友在Excel中使用VBA项目。 This project has a lot of Forms that pop up and perform various functionality while the spreadsheet is being used. 该项目有很多表单,在使用电子表格时弹出并执行各种功能。 Some of them have complex Form_Initialize methods that rely on other things already existing (this is not a problem when the project is used as expected). 其中一些具有复杂的Form_Initialize方法,这些方法依赖于已经存在的其他事物(当按预期使用项目时,这不是问题)。

We are trying to print out the names of every control on every form within the application. 我们试图在应用程序中的每个表单上打印出每个控件的名称。 Our problem is that the VBA.UserForms collection only contains forms that have already been instantiated, and we can't instantiate all the forms without their Form_Initialize methods executing. 我们的问题是VBA.UserForms集合只包含已经实例化的表单,并且我们无法在没有执行Form_Initialize方法的情况下实例化所有表单。

For example: 例如:

For Each f In VBA.UserForms
    Debug.Print f.Name
    Debug.Print "----------------------"

    For Each c In f.Controls
        Debug.Print c.Name
    Next c
Next f

does nothing if no forms have been used/loaded. 如果没有使用/加载表单,则不执行任何操作。 This code: 这段代码:

For Each c in frmConfig.Controls
    Debug.Print c.Name
Next c

First executes frmConfig.Form_Initialize() , then loops through the controls on the form printing their names. 首先执行frmConfig.Form_Initialize() ,然后遍历打印其名称的表单上的控件。 This crashes, as things that need to happen before this form is available have not happened. 这种崩溃,因为在此表单可用之前需要发生的事情尚未发生。

Is it possible to get the names of the controls on a form WITHOUT instantiating the form (avoiding execution of frmConfig.Form_Initialize() )? 是否可以在不实例化表单的情况下frmConfig.Form_Initialize()表单上控件的名称(避免执行frmConfig.Form_Initialize() )?

Any help much appreciated! 任何帮助非常感谢!

Is this what you are trying? 这是你在尝试什么?

Option Explicit

Sub FindObjects()
    Dim vbc As VBIDE.VBComponent
    Dim frm As Object
    Dim Ctrl As MSForms.Control

    For Each vbc In ThisWorkbook.VBProject.VBComponents
        If vbc.Type = vbext_ct_MSForm Then
            With VBA.UserForms
                On Error Resume Next
                Set frm = .Add(vbc.Name)
                Debug.Print "Found userform :" & vbc.Name
                If Err.Number = 0 Then
                    For Each Ctrl In frm.Controls
                        Debug.Print "Controls in Userform " & vbc.Name & _
                        " - " & Ctrl.Name
                    Next Ctrl
                End If
                On Error Go To 0
            End With
        End If
    Next vbc
End Sub

IMP : IMP

  1. Set reference to Microsoft Visual Basic For Applications Extensibility 设置对Microsoft Visual Basic For Applications Extensibility的引用
  2. In Excel options, set "Trust Access To the VBA project Object Model" 在Excel选项中,设置“信任访问VBA项目对象模型”

Screen Shot 屏幕截图

在此输入图像描述

FOLLOWUP 跟进

Since this is a one time thing, do this 由于这是一次性的事情,所以这样做

  1. Open VBA Project 打开VBA项目
  2. Press CTRL + F CTRL + F.
  3. Do as shown in the screenshot below and then run the code. 如下面的屏幕截图所示,然后运行代码。
  4. Close the file without saving once you have got what you need 一旦你得到了你需要的东西,关闭文件而不保存

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM