Merge Fields


MailMergeFields Collection Object

A collection of MailMergeField objects that represent the mail merge related fields in a document.
Use the Fields property to return the MailMergeFields collection.
The following example adds an ASK field after the last mail merge field in the active document.

ActiveDocument.MailMerge.Fields.Count 

Use Fields(index), where index is the index number, to return a single MailMergeField object.
The following example displays the field code of the first mail merge field in the active document.

MsgBox ActiveDocument.MailMerge.Fields(1).Code 


MailMergeField Object

Represents a single mail merge field in a document.
The MailMergeDataField object is a member of the MailMergeDataFields collection.
The MailMergeDataFields collection includes all the mail merge related fields in a document.



Inserting a MergeField


Use the Add method to add a merge field to the MailMergeFields collection.
The following example replaces the selection with a MiddleInitial merge field.

ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, _ 
                                    Name:="MiddleInitial"

Range - The range where you want the field to appear. This field replaces the range, if the range isn't collapsed.
Name - The name of the field.


Using the Fields Collection


ActiveDocument.Fields.Add Range:=Selection.Range, 
                          Type:=wdFieldMergeField, _
                          Text:="""Surname"""
                          PreserveFormatting:=True


MailMerge.Fields.AddAsk

The MailMergeFields collection has additional methods, such as AddAsk and AddFillIn, for adding fields related to a mail merge operation.

ActiveDocument.MailMerge.Fields.AddAsk Range:=Selection.Range, _ 
                                       Name:="Name",
                                       Prompt:="Type your name", _
                                       AskOnce:=True



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext