How to Create User-Defined Field?

R

Rob Bond

Hello,

I'm trying to create a Field (variable) called
"Product_Name" in my Word document (a report). The
"Product_Name" variable will have a single value throughout
the report, and may appear in many places, including the
document Header. When I create my next report I will update
the value of the "Product Name" variable.

This is **not** a mail merge situation where I'm creating
many reports at one time, but rather I'm simply having a
Word document serve as a report template. The report
template has a number of variables/Fields (like the
"Product_Name", "Manufacturer_Name", etc.) that I will
manually set at the start of each new report.

This seems really simple, but I've been spending way too
much time trying to figure this out. Would someone point me
in the right direction?

Thanks!

--Rob
 
G

Greg

Rob,

There are many ways to skin this cat.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top