引用控件的父对象

以下示例使用 Microsoft Forms 2.0 Parent 属性来引用包含特定控件或对象的控件、窗体或其他对象。

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

  • Two Label] (./../../api/Outlook.label.md) 名为 Label1 和 Label2 的控件。
  • CommandButton] (./../../api/Outlook.commandbutton.md) 名为 CommandButton1。
  • 一个或多个可选的其他控件。
Dim MyControl 
Dim MyParent 
Dim ControlsIndex 
 
Sub Item_Open() 
 Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1 
 ControlsIndex = 0 
 CommandButton1.Caption = "Get Control and Parent" 
 CommandButton1.AutoSize = True 
 CommandButton1.WordWrap = True 
End Sub 
 
Sub CommandButton1_Click() 
 Set Label1 = Item.GetInspector.ModifiedFormPages("P.2").Label1 
 Set Label2 = Item.GetInspector.ModifiedFormPages("P.2").Label2 
 
 'Process Controls collection for UserForm 
 Set MyControl = Item.GetInspector.ModifiedFormPages("P.2").Controls.Item(ControlsIndex) 
 Set MyParent = MyControl.Parent 
 Label1.Caption = MyControl.Name 
 Label2.Caption = MyParent.Name 
 
 'Prepare index for next control on Userform 
 ControlsIndex = ControlsIndex + 1 
 If ControlsIndex >= Item.GetInspector.ModifiedFormPages("P.2").Controls.Count Then 
 ControlsIndex = 0 
 End If 
End Sub

支持和反馈

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