Our office is moving from paper-based forms to online forms as part of our "Drive to Save the World." I've created online versions of most of the forms now, with plenty of space for people to fill in the relevant information. The trouble is, they tend to delete areas of the form that they think don't apply to them.
What you need is a Word form: a document that has fields for filling in information, and in which you can protect the areas that you don't want users to change.
To create a form, follow these general steps:
Create a document as usual, entering the necessary text, leaving gaps for the form fields that the users will fill in, and formatting the contents as needed.
Right-click the menu bar or any displayed toolbar and choose Forms from the pop-up menu to display the Forms toolbar (or choose View Toolbars Forms).
Double-click a form field (or select the field and click the Form Field Options button on the toolbar) to display the Form Field Options dialog box for the form field. Figure 5-1 shows the Text Form Field Options dialog box; the Check Box Form Field Options dialog box and the Drop-Down Field Options dialog box include checkbox- and drop-down-specific options but are otherwise almost identical. They include similar fields for running a macro on entry to and exit from the form field, the bookmark name for the field, a checkbox for specifying whether the field is enabled, and a checkbox for controlling whether Word calculates the field when the user leaves it.
To test your form, click the Protect Form button (the button with the lock icon) on the Forms toolbar. Word then makes only the form fields accessible. You can move the selection from one field to another by pressing Tab, Page Down, or to move forward, or Shift+Tab, Page Up, or to move backward. Click the Protect Form button again to remove the protection.
Save the document, and then make it available to the users who need to fill in the form. When a user opens the form, she can move quickly from one field to another, fill in the necessary information, and save the form under a name other than its current name (because the form is password-protected).
Right, protecting the form has prevented my colleagues from deleting the sections that don't apply to them. But they're still leaving some vital fields blank and filling in others incorrectly.
For a text form field, you can control what the user enters to some extent by specifying the type of text ("Regular text," "Number," "Date," "Current date," "Current time," or "Calculation") in the Type drop-down list in the Text Form Field Options dialog box. You can also control the default entry, the length of the entry, and the format in which it is entered.
For any form field, you can run a macro on entry (when the user moves the selection to the form field), on exit (when the user moves the selection to another form field), or both. An exit macro is especially useful for checking that the contents of the field are suitable and for displaying a message box explaining any problems to the user. (Chapter 8 discusses how to create macros.)
Exit macros are almost ideal for ensuring that the user fills in all the required fields in the formbut only almost. If the user uses the keyboard to move from field to field, the exit macros can validate each field. But if the user moves about the form with the mouse, he can skip fields altogether. When the user skips a field, the exit macro doesn't run, and there's no validation.
A straightforward approach is to force the user to work through the fields in the form in turn , validating the result of each field as the user leaves it. This approach has the advantage of alerting the user immediately to a problem with a field, but it means the user must work through the fields in order.
To validate each field as the user leaves it, create an exit macro for each field. Perform suitable validation (for example, check the Result property of a text FormField object to make sure that it's not an empty string) and display a message box explaining any problems that the user must fix before leaving the field. For each field except the last, make the exit macro, after finding the field's contents satisfactory, select the next form field (e.g., ActiveDocument.FormFields("FullName").Select selects the field with the bookmark name FullName ). The user can then fill in the next field. If the user tries to leave a field without completing it, the exit macro runs and tells him the problem.
If you're reluctant to constrict users to working through the form in order, validate the form's contents when the user submits it or saves it. If you add a Submit button from the Control Toolbox (choose View Toolbars Control Toolbox), you can attach to it a macro that performs the validation and notifies the user of any fields that were left blank. If you rely on the user saving the form, either create a macro named FileSave in the form that performs the validation, identifies any problems, and saves the form if all is well; or use the DocumentBeforeSave event to perform the validation.
Okay, I've got my colleagues filling in the form nowand they're filling in all the fields more or less correctly, which is great. The problem is, I need to extract the data from the form and dump it into our database. I guess I could write a macro to do so, but I don't know how.
Nor do you need to. Word can strip the data out of the form for you. Open the form template (or document), choose Tools Options, click the Save tab, and check the "Save data only for forms box.
Once you've done this, Word selects the "Plain text" item in the "Save as type" drop-down list when the user displays the Save dialog box. When the user clicks the Save button, Word displays the File Conversion dialog box, allowing the user to check the encoding of the text file before saving it. The resulting text file, which is in comma-separated value (CSV) format, contains only the text entered in the form and the values of any checkboxes (1 if checked, 0 if unchecked).