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
|
| Note: |
There is more
information about forms on the MVPS web
site. |