Graham Mayor

... helping to ease the lives of Microsoft Word users.

Many people access the material from this web site daily. Most just take what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved would help to ensure the continued availability of this resource. Click the appropriate button above to access PayPal.

Check for uncompleted form field

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.

Option Explicit
Private mstrFF As String
Public Sub AOnExit()
With GetCurrentFF
If Len(.Result) = 0 Then
MsgBox "You can't leave field: " & .Name & " blank" mstrFF = GetCurrentFF.Name
End If
End With
End Sub
Public Sub AOnEntry()
Dim strCurrentFF As String
If LenB(mstrFF) > 0 Then
ActiveDocument.FormFields(mstrFF).Select
mstrFF = vbNullString
End If
End Sub
Private Function GetCurrentFF() As Word.FormField
With Selection
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)
End If
End With
End Function

 

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"
Next
End Sub

 

Reset Form Fields

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.

Sub ResetFormFields()
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
ActiveDocument.Unprotect Password:=""
End If
For i = 1 To oFld.Count
With oFld(i)
.Select
If .name <> "" Then
Dialogs(wdDialogFormFieldOptions).Execute
End If
End With
Next
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
ofld(1).Select
End Sub

 

 

 

 

 

 

 

 

 

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

 

 

Validate form fields

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.