Anzeigen des Namens jedes Steuerelements auf einem Formular oder einer Seite eines MultiPage-Steuerelements

The following example uses the Item method to access individual members of the Microsoft Forms 2.0 Controls collection and Pages collection. The user chooses an option button for either the Controls collection or the MultiPage, and then clicks the CommandButton. Der Name des entsprechenden Steuerelements wird in der Bezeichnung zurückgegeben.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • Eine Befehlsschaltfläche ( CommandButton ) mit der Bezeichnung "CommandButton1".

  • Ein Bezeichnungsfeld-Steuerelement ( Label ) mit der Bezeichnung "Label1".

  • Zwei Optionsfeld-Steuerelemente ( OptionButton ) mit der Bezeichnung "OptionButton1" und "OptionButton2".

  • Ein MultiPage -Objekt mit der Bezeichnung "MultiPage1".

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

Support und Feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.