Need to place text in fields in a Word 2003 template

S

Salty Dog

An example field code is { SET CUST_NAME \* MERGEFORMAT }. I am using C# but
a VBA example would likely get me going. The fields are in table cells if
that makes a difference. I've tried a few different things but I can't get
anything to work. Thanks in advance!
 
J

Jay Freedman

An example field code is { SET CUST_NAME \* MERGEFORMAT }. I am using C# but
a VBA example would likely get me going. The fields are in table cells if
that makes a difference. I've tried a few different things but I can't get
anything to work. Thanks in advance!

Use the Add method of the Fields collection of the document. To insert your
example field at the beginning of the document, you could do this:

Sub demo()
Dim myRg As Range
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseStart

ActiveDocument.Fields.Add Range:=myRg, _
Type:=wdFieldSet, Text:="CUST_NAME", _
PreserveFormatting:=True
End Sub

Some programmers prefer to use Type:=wdFieldEmpty and then put the entire field
code into the Text parameter. I don't think it makes a difference either way.
 
Top