Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to Programmatically Manipulate a UserForm

View products that this article applies to.


The programming object model in Microsoft Office 97 for Windows allows you to create a custom dialog box (a UserForm) by using a Microsoft Visual Basic for Applications macro.

This article contains sample Visual Basic code that creates and manipulates a custom dialog box by using the programming object model.

↑ Back to the top

More information

The Visual Basic objects in Microsoft Office 97 provide the objects and methods you need to access Visual Basic projects and their elements. The top-level objects that controls the Visual Basic objects is the VBE object. The VBE object is the root object that contains all other objects and collections represented in Visual Basic for Applications. You can control the VBE object through the Application object. The VBE object contains the VBProject collection, which represents all the projects that are open in the development environment.

Each document also contains its own VBProject object, which is located under the Document or Workbook object.

The VBProject object contains the VBComponents collection. Dialog boxes, also called UserForms, are represented as UserForm objects. UserForm objects and code modules are elements of the VBComponents collection.

The Microsoft Visual Basic for Applications Extensibility Object Library

For the following Visual Basic code in this article to function, first load the "Microsoft Visual Basic for Applications Extensibility" object library in the project. This example uses Microsoft Excel, but the steps are similar for any of the Microsoft Office program. To add a reference to the "Microsoft Visual Basic for Applications Extensibility" library to your project, follow these steps:

  1. Start Microsoft Excel and create a new workbook.
  2. Press ALT+F11 to open the Visual Basic Editor.
  3. If the Project window is not visible, click Project Explorer on the View menu.
  4. In the Project window, click "VBAProject (Book1)."

    Note that the name of the workbook may vary.
  5. On the Insert menu, click Module.

    This step adds a module in the Book1 project.
  6. On the Tools menu, click References.
  7. Under Available References, click "Microsoft Visual Basic for Applications Extensibility" and click OK.
After you perform these steps, you can type the Visual Basic code that appears in this article in the new module and then run the macro.

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
173707 OFF97: How to run sample code from Knowledge Base articles

Macro to Count the Number of Visual Basic Components

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners -

Microsoft Advisory Services -

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:;EN-US;CNTACTMS To return the number of all modules, class modules, and UserForms in the active project in a message box, type the following code in the new module:
     Sub Count_VBComponents1()

      MsgBox Application.VBE.ActiveVBProject.VBComponents.Count

   End Sub
Or use the following code:
    Sub Count_VBComponents2()

      ' Replace "ActiveWorkbook" with "ActiveDocument" for Microsoft Word
      ' or "ActivePresentation" for Microsoft PowerPoint.
      MsgBox ActiveWorkbook.VBProject.VBComponents.Count

   End Sub

Creating a New UserForm

To create a new UserForm, use the Add method of the VBComponents collection and specify vbext_ct_MSDForm. For example, type the following code in the new module:
      Sub Add_Form1()

      ' Declare a variable to hold the UserForm.
      Dim x As Object

      ' Create a new UserForm. You can use this new VBComponent object
      ' to manipulate the User Form.
          Set x = Application.VBE.ActiveVBProject.VBComponents.Add _

   End Sub

Changing the Name and Other Properties of a UserForm

To change the name of a UserForm, change the value of its Name property. To change the names of other properties (for example, title, height, or width), change them through the Properties collection of the VBComponent object. The following example creates a new UserForm and then changes the name, caption (text that appears in the title bar), height, and width of the newly created dialog box:
  Sub Add_Form2()

      ' Declare a variable to hold the UserForm.
      Dim mynewform As Object

      ' Create a new UserForm. You can now use this new VBComponent object
      ' to manipulate the User Form.
      Set mynewform = _

      With mynewform
         .Properties("Height") = 246
         .Properties("Width") = 616
         .Name = "HelloWord"
         .Properties("Caption") = "This is a test"
      End With

   End Sub

Adding Controls to the UserForm

To add a new control to a UserForm, first use the Designer object of the corresponding VBComponent object. The Designer object allows you to manipulate the design of a UserForm. The Designer object contains a Controls collection. To add a new control, add it to the Controls collection. The following example adds a check box control to a newly created UserForm and sets the name, caption, position, and size of the control:
   Sub Add_Control()

      ' Declare variables.
      Dim mynewform As Object
      Dim mycheckbox As Object

      ' Create a new UserForm. You can use this new VBComponent object
      ' to manipulate the UserForm.
      Set mynewform = _
        Application.VBE.ActiveVBProject.VBComponents.Add (vbext_ct_MSForm)

      ' Add a checkbox to the new UserForm.
      Set myCheckBox = mynewform.Designer.Controls.Add("Forms.CheckBox.1")

      ' With the new checkbox...
      With myCheckBox
         .Name = "Check1"
         .Caption = "Check here"
         .Left = 10
         .Top = 10
         .Height = 20
         .Width = 60
      End With

   End Sub
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
157319 XL97: Problems when you use macro to add control to UserForm

↑ Back to the top


For additional information about getting help with Visual Basic for Applications, click the following article number to view the article in the Microsoft Knowledge Base:
163435 VBA: Programming resources for Visual Basic for Applications

↑ Back to the top

Keywords: KB185774, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 185774
Revision : 10
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 519