UserForms

Word UserForms are actually Office UserForms, which are hosted by most Office applications. They have a different object model than Word, so if you start working with them extensively, you will need to familiarize yourself with the Forms object library (MSForms in the Libraries drop-down box in the VBA window). But you can create and work with UserForms in the VBA window without using the Forms object model in code, and these forms are very useful for intermediate and advanced Word programming. UserForms allow you to go beyond a simple message box or input box and present users with a more extensive dialog box with (for example) combo boxes or list boxes where items can be selected, and text boxes for entering values.

To create a UserForm, open the VBA window, select the appropriate document or template in the Project Explorer, and click the Insert UserForm button on the toolbar. A new form opens (Figure 18-10), called UserForm1 (or a higher number, if there already are some forms for this template). When the UserForm has the focus, a Toolbox appears, with a selection of controls you can place on the form.

A newly created UserForm with several controls

Figure 18-10. A newly created UserForm with several controls

UserForms (and controls placed on them) have several useful events:

Initialize

The Initialize event of the form, which fires when the form is opened, is useful for filling in default values for controls on the ...

Get Word 2000 in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.