在多页控件的窗体或页面上显示每个控件的名称

以下示例使用 Item 方法访问 Microsoft Forms 2.0 Controls 集合和 Pages 集合的单个成员。 用户为 Controls 集合或 MultiPage 选择选项按钮,然后单击 CommandButton。 相应控件的名称在 Label 中返回。

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 一个名为"CommandButton1"的 CommandButton

  • 一个名为"Label1"的 Label

  • 两个名称分别为"OptionButton1"和"OptionButton2"的 OptionButton 控件。

  • 一个名为"MultiPage1"的 MultiPage

Dim ControlsIndex 
 
Sub CommandButton1_Click() 
 Set Controls = Item.GetInspector.ModifiedFormPages("P.2").Controls 
 Set OptionButton1 = Controls("OptionButton1") 
 Set OptionButton2 = Controls("OptionButton2") 
 Set Label1 = Controls("Label1") 
 Set MultiPage1 = Controls("MultiPage1") 
 
 If OptionButton1.Value = True Then 
 'Process Controls collection for UserForm 
 Set MyControl = Controls.Item(ControlsIndex) 
 Label1.Caption = MyControl.Name 
 
 'Prepare index for next control on Userform 
 ControlsIndex = ControlsIndex + 1 
 If ControlsIndex >= Controls.Count Then 
 ControlsIndex = 0 
 End If 
 
 ElseIf OptionButton2.Value = True Then 
 'Process Current Page of Pages collection 
 Set MyControl = MultiPage1.Pages.Item(MultiPage1.Value) 
 Label1.Caption = MyControl.Name 
 End If 
End Sub 
 
Sub Item_Open() 
 ControlsIndex = 0 
 
 Set OptionButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("OptionButton1") 
 Set OptionButton2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("OptionButton2") 
 Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CommandButton1") 
 
 OptionButton1.Caption = "Controls Collection" 
 OptionButton2.Caption = "Pages Collection" 
 OptionButton1.Value = True 
 
 CommandButton1.Caption = "Get Member Name" 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。