WD: How to Create a Form That Updates Automatically |
Q117209
The information in this article applies to:
-
Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
-
Microsoft Word for Windows 95, versions 7.0, 7.0a
-
Microsoft Word for the Macintosh, versions 6.0, 6.0.1, 6.0.1a
-
Microsoft Word 98 Macintosh Edition
SUMMARY
In Microsoft Word, you can create a form in which a section is updated automatically based on information that you type in or select from a form field. For example, if you want a user to select from a list of company names, and you want that company's address to appear in another section of your form, you
can set up your form by using AutoText entries, a bookmark, and an on-exit macro.
This article demonstrates how to create a form that contains a form field that causes a company's address (which is stored as an AutoText entry) to be inserted automatically when the user selects a company name from
a drop-down list and then moves to the next field on the form.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either
expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes
that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can
help explain the functionality of a particular procedure, but they will not
modify these examples to provide added functionality or construct procedures to
meet your specific needs. If you have limited programming experience, you may
want to contact a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft Certified
Partners, please see the following page on the World Wide Web:
http://www.microsoft.com/partner/referral/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://support.microsoft.com/directory/overview.asp
In the example that follows, you will create a form that contains a form
field that causes a company's address to be inserted automatically when
the user enters a form field and selects a company name. The address will
be placed at a bookmark located in an unprotected section of the document.
How to Create the On-Exit Macro
Create the macro appropriate for your version of Word, where "DropDown1" is
the bookmark that represents the drop-down form field containing your list
of items, and "Mark1" is the bookmark that represents where you want the
AutoText inserted.
Word 98 Macintosh Edition
Sub DropDownAutoText1() 'This is the name of the macro
Dim DropResult As String
' Get result of the form field.
DropResult = ActiveDocument.FormFields("DropDown1").Result
' UnProtect the document.
ActiveDocument.UnProtect
With Selection
' Go to location to insert Autotext
.GoTo What:=wdGoToBookmark, Name:="Mark1"
' Insert AutoText name chosen from dropdown list.
.InsertAfter DropResult
' Insert the AutoText.
.Range.InsertAutoText
End With
' Re-Protect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
Word 6.0 and 7.0
Sub Main
' Get result of the form field.
A$ = GetFormResult$ ("DropDown1")
' UnProtect the document.
ToolsUnprotectDocument
' Go to location to insert Autotext
EditGoTo .Destination = "Mark1"
' Insert AutoText name chosen from dropdown list.
Insert A$
' Insert the AutoText.
InsertAutoText
' Re-Protect the document.
ToolsProtectDocument .NoReset = 1
End Sub
For more information about the GetFormResult() function, see page 494 of
the "Microsoft Word Developer's Kit" for version 6.0 and page 557 of the
"Microsoft Word Developer's Kit, rev. 3."
How to Create the Drop-Down Form Field
Word 98 Macintosh Edition
Open a new document and follow these steps to create a drop-down form
field:
- On the View menu, point to Toolbars, and then click Forms.
- On the Forms toolbar, click Drop-Down Form Field (the third
button).
- Double-click the newly inserted form field. Enter an item
name in the Drop-down Item box, and choose the Add button. Use
items (such as company names) to denote information (such as
addresses) to be inserted into other places on the form.
- Repeat step 3 for each item needed in the Drop-Down list. Then
click OK.
- Right-click the Drop-Down Form Field and click Properties on
the shortcut menu.
- From the Drop-Down Form Field Options dialog box, under "Run
Macro On," in the Exit box, select the macro you created
in the "Create the On-Exit Macro" section of this article.
Click OK.
Word 6.0/7.0
Open a new document and follow these steps to create a drop-down form
field:
- On the Insert menu, click Form Field, and then select Drop-Down
from the Type list.
- Click the Options button. Enter an item name in the Drop-Down Item
box, and click the Add button. Use items (such as company names) to
denote information (such as addresses) to be inserted into other
places on the form.
- Repeat step 2 for each item needed in the Drop-Down list. Then click
OK.
- Right-click the Drop-Down Form Field, and then click Form Field Options.
- In the Form Field Drop-Down Options dialog box, under "Run Macro On,"
select the macro you created in the "Create the On-Exit Macro"
section of this article. Click OK.
For more information about creating form fields, see "Forms" beginning
on page 305 in the "Microsoft Word User's Guide" for version 6.0.
How to Create the AutoText Entries
Create the AutoText entries. Give each AutoText entry the same name that
is used in the drop-down form field list. The content of the AutoText
entry will be the company address.
For additional information on creating AutoText entries, use Word Help. To
locate the appropriate Help topic use the appropriate procedure for your
version of Word.
Word 98 Macintosh Edition
Follow these steps:
- On the Help menu, click Contents and Index.
- Click the Index tab, and then type AutoText on the Step 1
line.
- Under Step 2, select Creating from the AutoText topic, and
then click Display.
Word 7.x
- On the Help menu, click Microsoft Word Help Topics.
- Select the Index tab, and then type AutoText.
- Under AutoText, Select "creating entries," and then click
Display.
- Select the "Use shortcuts to insert frequently used text or
graphics" topic.
Word 6.x
- On the Help menu, click "Search for help on."
- Click the Index tab, and then type AutoText.
- Under AutoText, select "creating entries," and then click Display.
- Click the "Use shortcuts to insert frequently used text or graphics"
topic.
How to Place the Bookmark in the Form
Use the appropriate procedure to insert a bookmark (in this example, name
the bookmark Mark1) where you want the AutoText (address) to appear. This
section of the document will be unprotected so that the AutoText result
can be inserted into the document. Insert a Continuous Section Break above
and below this bookmark.
Word 98 Macintosh Edition
Follow these steps:
- On the Insert menu, click Bookmark.
- Type Mark1 on the Bookmark name line.
- Click Add.
Word 6.x and 7.x
Follow these steps:
- On the Edit menu, click Bookmark.
- Type Mark1 on the Bookmark Name line.
- Click Add.
NOTE: You will not be able to use bookmarks in headers, footers,
annotations, footnotes, or endnotes because the EditGoto command in the
macro will not see these bookmarks, and they cannot be unprotected in
your form.
How to Protect the Form
To protect the form, follow these steps:
- On the Tools menu, click Protect document, select the Forms option, and
click Sections.
- In the Section Protection dialog box, clear the check box corresponding
to the section where the AutoText will be inserted and click OK twice.
- Save the Form.
REFERENCES
"Microsoft Word Developer's Kit," for version 6.0, pages 494
[GetFormResult() function], 681 (SetFormResult statement)
"Microsoft Word User's Guide," version 6.0, pages 305-326 ("Forms"), 743-
755 ("Recording and Running Macros")
Additional query words:
forms result module auto text