The Problem:
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.
The Solution:
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.
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.