word field codes

T

Tony

Is there a way in Word to enter in text that when changed in one part of a
document the text is automatically updated throughout the rest of the doc?
Example: if I have "document revision number 1.01" on the first page of the
document can I copy and paste this text throughout the doc and when I change
the text on the first page to "document revision number 1.02", the text
previously pasted will automatically change from "document revision number
1.01" to "document revision number 1.02"

Thanks.
 
G

Greg Maxey

Tony,

One method is a UserForm. For tips getting started see:
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

If you don't want to go that route there are several other
approaches.

1. ASK and REF Fields. ASK fields fire a prompt and
assign a bookmark name. REF fields return the bookmark
value. Say you have { ASK ProductName }{ ProductName} on
page one and {ProductName } on page 2. When ASK fields
are updated the prompt fires and REF field update. You
need one ASK field for each variable. Use as many REF
fields as necessary whereever the data needs to be
repeated.

A disadvantage of ASK and REF fields is ASK field don't
update (fire the prompt) automatically when the document
opens, and will (annoying) fire when you go to print if
File>Print>Options>Update field is checked.

You can force ASK fields to update when a document is
created and opened with the following code in AutoNew and
AutoOpen macros:


Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType < wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing

2. You could use existing DOCPROPERTY fields or create
your own DOCPROPERTY fields. Press
File>Properties>Summary. Type Test in the Subject field.
Go back to your docuemnt type Subject, select the word
subject and press CTRL+F9. You just created a docproperty
field. Right click it and press update field. Test
appears. Put docproperty subject fields where every you
want it repeated. Now a custom field.
File>Properties>Custom. Type ProductName in the name
field. Type A better mouse trap in the value field. CLick
OK. In the document type DOCPROPERTY ProductName. Select
it and press CTRL+F9. Right click and select update field.

Lets say you have report template that already has custom
document properties for employee name and badge number.
You can automate assigning docproperty values with a
couple of macros:

Sub AutoNew()
Dim EmpName As String
Dim BadgeNum As String
EmpName = InputBox("Enter employee
name", "Employee Name", "John Q. Sample")
BadgeNum = InputBox("Enter badge number", "Badge
Number", "0000")
ActiveDocument.CustomDocumentProperties("Employee
Name").Value = EmpName
ActiveDocument.CustomDocumentProperties("Badge
Number").Value = BadgeNum
ActiveDocument.Fields.Update
End Sub

Sub AutoOpen()

Dim Update As VbMsgBoxResult
Dim PolicyNum As String
Dim ClientName As String
On Error GoTo ExitSub
Update = MsgBox("Update Document Information?", vbYesNo)
If Update = vbYes Then
EmpName = InputBox("Enter employee name", "Employee
Name", "John Q. Sample")
BadgeNum = InputBox("Enter badge number", "Badge
Number", "0000")
ActiveDocument.CustomDocumentProperties("Employee
Name").Value = EmpName
ActiveDocument.CustomDocumentProperties("Badge
Number").Value = BadgeNum
ActiveDocument.Fields.Update
End If
ExitSub:
End Sub

HTH
 
J

Jezebel

Simplest is to use document properties. Enter the data on the File >
Properties dialog, using either one of the built-in properties on the
Summary tab, or one of your own naming on the Custom tab.

To put the value into the document, use DocProperty fields. You have to
UpdateFields to refresh the display in the document after you set or change
the property value. A big advantage of document properties is that you can
easily read and change them through automation, without interfering with the
content of the document.

Another method is to assign a bookmark to the first instance of the text --
where you actually type it in the document -- then use REF fields elsewhere
to refer to the bookmark's content. This requires a little more discipline
on the user's part because if you delete or replace the range containing the
bookmark, you delete the bookmark also.
 
Top