フォームまたは MultiPage コントロールのページに各コントロールの名前を表示する

次の例は、 Item メソッドを使用して、Microsoft Forms 2.0 Controls コレクションと Pages コントロールの個別のメンバーを操作します。 ユーザーは 、Controls コレクションまたは MultiPage のいずれかのオプション ボタンを選択し、 CommandButton をクリックします。 適切なコントロールの名前が Label に返されます。

この例を利用するには、次のコード例をフォームのスクリプト エディターにコピーします。 コードを実行するには、 Open イベントが生じるようにフォームを開く必要があります。 フォームには次のコントロールが含まれている必要があります。

  • CommandButton1 と命名された CommandButton

  • ラベル ( Label ) コントロール (Label1)

  • 2 つのオプション ボタン ( OptionButton ) コントロール (OptionButton1 と OptionButton2)

  • マルチ ページ ( MultiPage ) コントロール (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

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。