Automating Forms

Macros can be used to automate the process of filling in Word forms. Information that would otherwise have to be typed manually can be filled in automatically. Custom commands can be created that are appropriate for a particular form.

Several types of macros are useful for automating forms. They include the following:

A form should be made into a template so that whenever a user wants to fill in a new form, he or she creates a new document based on the form template. The new form inherits the form's boilerplate text and formatting. Any macros used to automate the form can be stored in the template. If the template is protected for forms, so are new documents based on it.

Here are some other points to keep in mind when automating a form:

For a complete list of form statements and functions, see "Language Summary" in Part 2, "WordBasic Reference."

Example

This form, including the macros described here, is available as INVOICE2.DOT (Windows) or INVOICE FORM (Macintosh) on the Microsoft Word Developer's Kit disk.

When a salesperson creates a new invoice, an AutoNew macro inserts a number for the Invoice Number and Customer's P.O. Number, and updates fields that indicate the Invoice Date, Date Ordered, and Date Shipped.

The new document is protected for forms, so the salesperson can type in form fields only and can't inadvertently disturb other parts of the form. Because the form is protected, the salesperson can use the tab key to move from one form field to another.

When the new invoice is created, the first form field — the New Customer check box — is automatically selected. When the user moves to the first form field in the address, an on-entry macro runs. The on-entry macro evaluates whether the New Customer check box is selected or cleared. If the check box is selected, indicating a new customer, the macro displays the New Customer dialog box as shown in the following illustration.

If the New Customer check box is cleared, indicating an existing customer, the macro displays a dialog box that allows the salesperson to query a Microsoft Excel database for the customer's address. He or she can type any part of the customer's name to get a list of customer names that match.

If a match is found, the macro displays a dialog box in which the salesperson can select the address he or she wants.

The macro then places the information in the Sold To area of the form. The salesperson can select the type of terms and shipping method from drop-down form field lists.

When the salesperson moves into the Quantity column, an on-entry macro displays the following dialog box showing a list of the items available.

When the salesperson selects the item and quantity, the macro then inserts this information into the form, along with the corresponding part number, unit price, and total amount. Each item selected by the salesperson is entered on a separate line of the form.

When the salesperson has entered all the items ordered in the invoice, he or she then chooses the Total Orders button in the Order Items dialog box. The macro adds the figures in the Total Amount column, calculates the sales tax (if any), and inserts the appropriate figures.

When the salesperson has completed the form, it is printed out and the data is saved. The modified Close command (File menu) sends the form data to a Microsoft Excel database that stores a record for every invoice.