Article ID: 204330 - View products that this article applies to.
This article was previously published under Q204330
The following Microsoft Visual Basic for Applications (VBA) procedures allow you to create and manipulate a UserForm through VBA code. This article illustrates how to use code to add a text box, a check box, and a command button to a form.
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 - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
VBA ObjectsThe following sample code creates a UserForm, changes its properties, and adds controls to it.
For these VBA objects to work, you must reference the Microsoft Visual Basic for Applications Extensibility library. To load the library into the current VBA project, follow these steps:
The VBProject object, in turn, 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.
In Microsoft Word for Windows 2000, each document, as well as every template, can contain its own VBProject object. In the Word documents, the VBProject object is located underneath the Document object.
VBE Projects and ComponentsThe following sample code displays a message box for each of the components in the active project. Each message box displays the name of the component.
Use one of the following subprocedures to display a message box with the total number of components in the active project.
(In Microsoft Excel, replace ActiveDocument with ActiveWorkbook; in Microsoft PowerPoint, substitute ActivePresentation.)
Creating a New UserFormThe following sample code creates a new UserForm and assigns the variable MyNewForm to it. You can now use this new VBComponent object to manipulate the User Form.
Changing the Name and Other Properties of a UserFormThe following sample code creates a new UserForm and then changes the name, caption (text shown in the title bar), height, and width of the newly created dialog box.
Adding Controls to the UserFormThe following sample code adds a check box control to a newly created UserForm and sets the name, caption, position, and size of the control. To add a control to a UserForm, you must first access the Designer Property object of the corresponding VBComponent object. The Designer object (in this case, the ToolBox controls) is the object that permits manipulation and access to the controls' properties.
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/226118/EN-US/ )OFF2000: Programming Resources for Visual Basic for Applications
(http://support.microsoft.com/kb/237356/EN-US/ )WD2000: How to Access Sample Macros for Word 2000
(http://support.microsoft.com/kb/212536/EN-US/ )OFF2000: How to Run Sample Code from Knowledge Base Articles
Contact us for more help
Connect with Answer Desk for expert help.