Add related procedures in class MailMergeField (Word VBA)

This page presents procedures from class MailMergeField related to the theme Add: MailMergeFields.Add, MailMergeFields.AddAsk, MailMergeFields.AddFillIn, MailMergeFields.AddIf, MailMergeFields.AddMergeRec, MailMergeFields.AddMergeSeq, MailMergeFields.AddNext, MailMergeFields.AddNextIf, MailMergeFields.AddSet and MailMergeFields.AddSkipIf

MailMergeFields.Add

Returns a MailMergeField object that represents a mail merge field added to the data source document.

Syntax : expression.Add (Range, Name)

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

Arguments

The following arguments are required:

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

Name (String) - The name of the field.

MailMergeFields.AddAsk

Adds an ASK field to a mail merge main document. Returns a MailMergeField object.

When updated, an ASK field displays a dialog box that prompts you for text to assign to the specified bookmark.

Syntax : expression.AddAsk (Range, Name, Prompt, DefaultAskText, AskOnce)

Dim rngTemp As Range 
 
Set rngTemp = ActiveDocument.Content 
 
rngTemp.Collapse Direction:=wdCollapseEnd 
ActiveDocument.MailMerge.Fields.AddAsk _ 
 Range:=rngTemp, _ 
 Prompt:="Type your company name", _ 
 Name:="company", AskOnce:=True

Arguments

The following arguments are required:

Range (Range) - The location for the ASK field.

Name (String) - The bookmark name that the response or default text is assigned to. Use a REF field with the bookmark name to display the result in a document.

The following arguments are optional

Prompt (String) - The text that's displayed in the dialog box.

DefaultAskText (String) - The default response, which appears in the text box when the dialog box is displayed. Corresponds to the \d switch for an ASK field.

AskOnce (Boolean) - True to display the dialog box only once instead of each time a new record is merged. Corresponds to the \o switch for an ASK field.

MailMergeFields.AddFillIn

Adds a FILLIN field to a mail merge main document. Returns a MailMergeField object.

When updated, a FILLIN field displays a dialog box that prompts you for text to insert into the document at the location of the FILLIN field. Use the Add method with the Fields collection object to add a FILLIN field to a document other than a mail merge main document.

Syntax : expression.AddFillIn (Range, Prompt, DefaultFillInText, AskOnce)

With Selection 
 .Collapse Direction:=wdCollapseStart 
 .InsertAfter "Name: " 
 .Collapse Direction:=wdCollapseEnd 
End With 
ActiveDocument.MailMerge.Fields.AddFillin Range:=Selection.Range, _ 
 Prompt:="Your name?", DefaultFillInText:="Joe", AskOnce:=True

Arguments

The following argument is required

Range (Range) - The location for the FILLIN field.

The following arguments are optional

Prompt (Dialog) - The text that's displayed in the dialog box.

DefaultFillInText (String) - The default response, which appears in the text box when the dialog box is displayed. Corresponds to the \d switch for an FILLIN field.

AskOnce (Boolean) - True to display the prompt only once instead of each time a new record is merged. Corresponds to the \o switch for a FILLIN field. The default value is False.

MailMergeFields.AddIf

Adds an IF field to a mail merge main document. Returns a MailMergeField object.

When updated, an IF field compares a field in a record with a specified value, and then it inserts the appropriate text according to the result of the comparison.

Syntax : expression.AddIf (Range, MergeField, Comparison, CompareTo, TrueAutoText, TrueText, FalseAutoText, FalseText)

ActiveDocument.MailMerge.Fields.AddIf Range:=Selection.Range, _ 
 MergeField:="Company", Comparison:=wdMergeIfIsBlank, _ 
 TrueText:="for your personal use", _ 
 FalseText:="for your business"

Arguments

The following arguments are required:

Range (Range) - The location for the IF field.

MergeField (String) - The merge field name.

Comparison (WdMailMergeComparison) - The operator used in the comparison.

Possible Values are

wdMergeIfEqual A value is output if the mail merge field is equal to a value.
wdMergeIfGreaterThan A value is output if the mail merge field is greater than a value.
wdMergeIfGreaterThanOrEqual A value is output if the mail merge field is greater than or equal to a value.
wdMergeIfIsBlank A value is output if the mail merge field is blank.
wdMergeIfIsNotBlank A value is output if the mail merge field is not blank.
wdMergeIfLessThan A value is output if the mail merge field is less than a value.
wdMergeIfLessThanOrEqual A value is output if the mail merge field is less than or equal to a value.
wdMergeIfNotEqual A value is output if the mail merge field is not equal to a value.

The following arguments are optional

CompareTo (String) - The text to compare with the contents of MergeField.

TrueAutoText (String) - The AutoText entry that's inserted if the comparison is true. If this argument is specified, TrueText is ignored.

TrueText (String) - The text that's inserted if the comparison is true.

FalseAutoText (String) - The AutoText entry that's inserted if the comparison is false. If this argument is specified, FalseText is ignored.

FalseText (String) - The text that's inserted if the comparison is false.

MailMergeFields.AddMergeRec

Adds a MERGEREC field to a mail merge main document. Returns a MailMergeField object.

A MERGEREC field inserts the number of the current record (the position of the record in the current query result) during a mail merge.

Syntax : expression.AddMergeRec (Range)

Range: The location for the MERGEREC field.

Dim rngTemp As Range 
 
Set rngTemp = ActiveDocument.Range(Start:=0, End:=0) 
 
ActiveDocument.MailMerge.Fields.AddMergeRec Range:=rngTemp 
rngTemp.InsertAfter "Record Number: "

MailMergeFields.AddMergeSeq

Adds a MERGESEQ field to a mail merge main document. Returns a MailMergeField object.

A MERGESEQ field inserts a number based on the sequence in which records are merged (for example, when record 50 of records 50 to 100 is merged, MERGESEQ inserts the number 1).

Syntax : expression.AddMergeSeq (Range)

Range: The location for the MERGESEQ field.

Dim rngTemp As Range 
 
Set rngTemp = ActiveDocument.Content 
 
rngTemp.Collapse Direction:=wdCollapseEnd 
ActiveDocument.MailMerge.Fields.AddMergeSeq Range:=rngTemp 
rngTemp.InsertAfter "Sequence Number: "

MailMergeFields.AddNext

Adds a NEXT field to a mail merge main document. Returns a MailMergeField object.

A NEXT field advances to the next record so that data from more than one record can be merged into the same merge document (for example, a sheet of mailing labels).

Syntax : expression.AddNext (Range)

Range: The location for the NEXT field.

Documents("Main.doc").MailMerge.Fields(3).Select 
Selection.Collapse Direction:=wdCollapseEnd 
Documents("Main.doc").MailMerge.Fields.AddNext _ 
 Range:=Selection.Range

MailMergeFields.AddNextIf

Adds a NEXTIF field to a mail merge main document. Returns a MailMergeField object.

A NEXTIF field compares two expressions, and if the comparison is true, the next record is merged into the current merge document.

Syntax : expression.AddNextIf (Range, MergeField, Comparison, CompareTo)

Documents("Main.doc").MailMerge.Fields(1).Select 
Selection.Collapse Direction:=wdCollapseStart 
Documents("Main.doc").MailMerge.Fields.AddNextIf _ 
 Range:=Selection.Range, MergeField:="PostalCode", _ 
 Comparison:=wdMergeIfEqual, CompareTo:="98004"

Arguments

The following arguments are required:

Range (Range) - The location for the NEXTIF field.

MergeField (String) - The merge field name.

Comparison (WdMailMergeComparison) - The operator used in the comparison.

Possible Values are

wdMergeIfEqual A value is output if the mail merge field is equal to a value.
wdMergeIfGreaterThan A value is output if the mail merge field is greater than a value.
wdMergeIfGreaterThanOrEqual A value is output if the mail merge field is greater than or equal to a value.
wdMergeIfIsBlank A value is output if the mail merge field is blank.
wdMergeIfIsNotBlank A value is output if the mail merge field is not blank.
wdMergeIfLessThan A value is output if the mail merge field is less than a value.
wdMergeIfLessThanOrEqual A value is output if the mail merge field is less than or equal to a value.
wdMergeIfNotEqual A value is output if the mail merge field is not equal to a value.

The following argument is optional

CompareTo (String) - The text to compare with the contents of MergeField.

MailMergeFields.AddSet

Adds a SET field to a mail merge main document. Returns a MailMergeField object.

A SET field defines the text of the specified bookmark.

Syntax : expression.AddSet (Range, Name, ValueText, ValueAutoText)

Dim rngTemp as Range 
 
Set rngTemp = ActiveDocument.Range(Start:=0, End:=0) 
 
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _ 
 Name:="Name", ValueText:="Joe Smith" 
Selection.Collapse Direction:=wdCollapseEnd 
ActiveDocument.Fields.Add Range:=Selection.Range, _ 
 Type:=wdFieldRef, Text:="Name"

Arguments

The following arguments are required:

Range (Range) - The location for the SET field.

Name (String) - The bookmark name that ValueText is assigned to.

The following arguments are optional

ValueText (String) - The text associated with the bookmark specified by the Name argument.

ValueAutoText (String) - The AutoText entry that includes text associated with the bookmark specified by the Name argument. If this argument is specified, ValueText is ignored.

MailMergeFields.AddSkipIf

Adds a SKIPIF field to a mail merge main document. Returns a MailMergeField object.

A SKIPIF field compares two expressions, and if the comparison is true, SKIPIF moves to the next record in the data source and starts a new merge document.

Syntax : expression.AddSkipIf (Range, MergeField, Comparison, CompareTo)

Documents("Main.doc").MailMerge.Fields(1).Select 
Selection.Collapse Direction:=wdCollapseStart 
Documents("Main.doc").MailMerge.Fields.AddSkipIf _ 
 Range:=Selection.Range, MergeField:="PostalCode", _ 
 Comparison:=wdMergeIfEqual, CompareTo:="98040"

Arguments

The following arguments are required:

Range (Range) - The location for the SKIPIF field.

MergeField (String) - The merge field name.

Comparison (WdMailMergeComparison) - The operator used in the comparison.

Possible Values are

wdMergeIfEqual A value is output if the mail merge field is equal to a value.
wdMergeIfGreaterThan A value is output if the mail merge field is greater than a value.
wdMergeIfGreaterThanOrEqual A value is output if the mail merge field is greater than or equal to a value.
wdMergeIfIsBlank A value is output if the mail merge field is blank.
wdMergeIfIsNotBlank A value is output if the mail merge field is not blank.
wdMergeIfLessThan A value is output if the mail merge field is less than a value.
wdMergeIfLessThanOrEqual A value is output if the mail merge field is less than or equal to a value.
wdMergeIfNotEqual A value is output if the mail merge field is not equal to a value.

The following argument is optional

CompareTo (String) - The text to compare with the contents of MergeField.