OFF97: How to Programmatically Manipulate a UserForm

ID: Q185774

The information in this article applies to:

SUMMARY

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.

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, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q173707
   TITLE     : 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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 _
          (vbext_ct_MSForm)

   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 = _
      Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_MSForm)

      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, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q157319
   TITLE     : XL97: Problems When you use Macro to Add Control to UserForm

REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: vba
Keywords          : kbdta kbdtacode xlvbahowto 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 7, 1999