Check for
uncompleted form field
|
|
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.
First step is to open the vba editor
and create a new module in the document template (or in normal.dot). In
the following illustration, I have created the module in normal.dot and
renamed it from the default Module1 to FieldExitMacro. The name isn't
particularly important. |
|
 |
| |
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()
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 pick the template from the User Templates or
Workgroup folder). 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 below: |
|

|
|
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 |