How to: Add a Control to a Page

Office 2013 and later

Contribute to this content

Use GitHub to suggest and submit changes. See our guidelines for contributing to VBA documentation.

The following example uses the Add method of the Microsoft Forms 2.0 Controls collection to add a control to a form at run time.

To use this example, copy this sample code to the Script Editor of a form. Make sure that the form contains:

Dim Mycmd 
Sub CommandButton1_Click() 
 Set Mycmd = Item.GetInspector.ModifiedFormPages("P.2").Controls.Add("Forms.CommandButton.1") ', CommandButton2, Visible) 
 Mycmd.Left = 18 
 Mycmd.Top = 150 
 Mycmd.Width = 175 
 Mycmd.Height = 20 
 Mycmd.Caption = "This is fun." & Mycmd.Name 
End Sub