|
This Microsoft Word Help & Tips page will show you how to
“master” form fields and put them to work for you.
The basic form fields are text fields, checkboxes, and
dropdown fields. Word assigns a bookmark name to each form field when it is
created. The default bookmark names are Text1, Check1, and DropDown1
respectively. The default name is applied with the next sequential number as
each new field is added.
(Note – fields are not renamed automatically when
fields are deleted or inserted between existing fields. See: Rename
Formfields Globally)
You can see the bookmark name assigned to any field when
the form is unprotected by double clicking on the field opening Form Field
Options dialog box. |
|
|

|
|
|
When the form is protected you can use a macro to determine
the bookmark name of the selected form field: |
|
|
Sub GetFldBkMrkName()
If Selection.FormFields.Count = 1 Then
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
End Sub |
|
|
When you open a Form Field Options dialog box, notice the two windows labeled “Run Macro on” “Entry” and “Exit”(See - the
display above). These and the macros they designate are key collaborators in
controlling form field behavior.
With a few simple macros you can
figuratively make your forms “sing and dance”
I am not very good at dreaming up practical examples, so I
am simply going to give some fanciful examples of how you can put form fields to
work for you. It is up to you to come up with something practical. Fair enough? |
|
First open a new document. Open the Forms toolbar and
insert the following:
1. Two Text Form Fields
2. Two Check Box Form Fields
3. A Drop-Down Form Field |
|

|
|
Finally, click in the
document immediately after the two text form fields that you entered,
type a few spaces and then click Insert>Bookmark. Type in the "Bookmark
Name" field myBkMrk and
press “Add.” |
|
Ok just to check, click on Insert>Bookmarks to confirm your new document now
contains the following seven bookmarks: |
|

|
|
Ok now for some examples.
1. You can use the text in
one form field to set the text in another form field or define the text of a
bookmark (Note – you can do the same thing using the result of a checkbox or
dropdown field).
Lets assume that the label
preceding Text1 is “Enter your last name:” and the label preceding Text2 is
“Enter your spouses last name:”
Set the following macro to
“Run on Exit” from the Text1: |
|
|
Sub ROEx1()
Dim oFFld As FormFields
Dim oBkMrk As Bookmarks
Dim oBkMrkRng As Word.Range
Set oFFld = ActiveDocument.FormFields
Set oBkMrk = ActiveDocument.Bookmarks
ActiveDocument.Unprotect
If oFFld("Text1").Result = "" Then
'Identify current Bookmark range
and insert text
Set oBkMrkRng = oBkMrk("myBkMrk").Range
oBkMrkRng.Text = "Text1 field can not be left blank"
'Re-insert the bookmark
oBkMrk.Add "myBkMrk", oBkMrkRng
oFFld("Text2").Result = ""
Else
oFFld("Text2").Result = " "
Set oBkMrkRng = oBkMrk("myBkMrk").Range
oBkMrkRng.Text = " "
'Re-insert the bookmark
oBkMrk.Add "myBkMrk", oBkMrkRng
oFFld("Text2").Result = oFFld("Text1").Result
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
End Sub |
|
|
Set the following macro to “Run on Entry” to
Text2: |
|
|
Sub ROEn1()
Dim oFFld As FormFields
Dim oBkMrk As Bookmarks
Set oFFld = ActiveDocument.FormFields
Set oBkMrk = ActiveDocument.Bookmarks
If oFFld("Text1").Result = "" Then
oBkMrk("Text1").Range.Fields(1).Result.Select
End If
End Sub |
|
|
Protect the form. |
|
Unless you are a rapper like
50Cent or some other extreme entertainer, you will have a last name. If
not, just enter your name. The odds still favor that your spouse’s last
name matches yours. The preceding macros require a last name entry and
assume your spouse matches yours. You can change the spouse last name, but
you can’t leave Text1 blank.
Give
it a try. The following two displays illustrates how the two macros
collaborate to ensure the fields are properly completed. |
|

|
|

|
|
Fellow MVP Graham Mayor offers extensive coverage of
“validating” form fields in his website article:
Check for uncompleted
formfield.
2. You can use a checkbox result to set the result
other checkboxes (Note – you also set text field and dropdown field results
based on checkbox results).
Unprotect the form and set the following macro to “Run
on Exit” from the Check1: |
| |
Sub ROEx2()
Dim oFFld As FormFields
Set oFFld = ActiveDocument.FormFields
If oFFld("Check1").CheckBox.Value = True Then
oFFld("Check2").CheckBox.Value = True
Else
oFFld("Check2").CheckBox.Value = False
End If
End Sub |
|
|
Protect the form and confirm that check1 controls
check2
(Note – Form control is limited to On Entry and On Exit
methods only. There is no way you can click a check box and see
instantaneous changes in other fields. Also, and very unfortunately, there
is no comprehensive method to make check boxes mutually exclusive. The following link to open and article on the MVP FAQ site that provides a
limited work around for creating mutually exclusive check boxes:
MVP
FAQ Article) |
|
3. You can use the result of a dropdown to control the
display of the dropdown (Note – you can also control text field results,
check box results or other dropdown results from a dropdown result).
Unprotect the form, double-click the dropdown1 field
and add the following items.” |
|

|
|
Now set the following macro to “Run on Exit” from the
Dropdown1: |
| |
Sub ROEx3()
Dim oFFld As FormFields
Set oFFld = ActiveDocument.FormFields
Select Case oFFld("DropDown1").Result
Case "Automatic"
ActiveDocument.Range.Font.Color =
wdColorAutomatic
Case "Red"
ActiveDocument.Range.Font.Color =
wdColorRed
Case "Blue"
ActiveDocument.Range.Font.Color =
wdColorBlue
Case "Green"
ActiveDocument.Range.Font.Color =
wdColorGreen
Case Else
'Do nothing
End Select
End Sub |
|
|
Protect the form. Ready, set, give it a try!! |
|

|
|

|
|
For the final example lets suppose that you want to control the options
available in one dropdown box based on the selection from another dropdown
box. To keep it simple, lets assume the first dropdown box options are
"A" and "B." You want the second dropdown box options to be words
starting with "A" or starting with "B." |
|

|

|
|
To achieve this feat you will need to run a macro on exit from the first
dropdown that will redefine the ListEntries in the second dropdown field.
Something like this will do: |