First step is to open the vba editor and create a new module in the document template (or in the 'Normal' template). In the following illustration, I have created the module in the Normal template and renamed it from the default Module1 to FieldExitMacro. The name isn't particularly important as long as it is unique to that template.
Copy and paste the following macro code into the module you have created, save and close the macro editor.
Private mstrFF As String
Public Sub AOnExit()
If Len(.Result) = 0 Then
MsgBox "You can't leave field: " & .Name & " blank" mstrFF = GetCurrentFF.Name
Public Sub AOnEntry()
Dim strCurrentFF As String
If LenB(mstrFF) > 0 Then
mstrFF = vbNullString
Private Function GetCurrentFF() As Word.FormField
If .FormFields.Count = 1 Then
' CheckBox or DropDown
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
If it is not already open, open your form template for editing (File > Open and select the template. If the form is locked, unlock it and right click the field you wish to validate. Select 'properties' and complete the entry and exit macros as shown in the illustration below:
Word 2007 and 2010 do not provide easy access to the legacy form fields and the protect/unprotect button. You could simply add the 'Lock' button from the All Commands group to the QAT (Quick Access Toolbar) but if you are working regularly with protected forms I would recommend that you investigate the forms toolbar add-in from Greg Maxey's web site. which reproduces the familiar forms toolbar from older Word versions in a readily accessible format.
Repeat for every other field as required
If only some of the fields require validation, note that the AOnEntry macro must be applied to all the other fields.
The following macro code will assign the entry macros to all fields (and, if you remove the apostrophe from the beginning of the line, it will apply the exit macros also), then before locking the form again, ensure that the cursor is in the first field to be filled, as you will not be able to tab out of the last field you edited without entering data in it.
Public Sub SetupMacros()
Dim ffItem As Word.FormField
For Each ffItem In ActiveDocument.FormFields
ffItem.EntryMacro = "AOnEntry"
'ffItem.ExitMacro = "AOnExit"
The following macro will restore all the named form fields in protected form to their default state as defined in the field properties.
The command line Dialogs(wdDialogFormFieldOptions).Execute will throw an error if the form field is unnamed. As it is poor practice to use unnamed form fields, I have not provided a workaround.
Dim bProtected As Boolean
Dim oFld As FormFields
Dim i As Long
Set oFld = ActiveDocument.FormFields
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
For i = 1 To oFld.Count
If .name <> "" Then
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
Note: There is more information about forms on the MVPS web site. and there are more macro examples on this site at Insert content from a form field, Extract data from protected forms and Some Useful Word Macro Examples
It is not unreasonable when preparing on-line forms using the forms toolbar, that you require the user to actually complete the fields that you have included. There is no function in the field properties to force this requirement, but you can do so with the aid of a few small macros. To this end I have to thank Peter Hewett, a regular Word newsgroup contributor, for developing the code featured here.