Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / July 2004

Tip: Looking for answers? Try searching our database.

How to Create User-Defined Field?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob Bond - 27 Jul 2004 16:55 GMT
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
Greg - 27 Jul 2004 19:47 GMT
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  

 

>-----Original Message-----
>Hello,
[quoted text clipped - 21 lines]
>--Rob
>.
Rob Bond - 28 Jul 2004 11:32 GMT
Thanks Greg, that's prefect.

--Rob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.