MailMergeFields.AddIf method (Word)

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

Syntax

expression. AddIf( _Range_ , _MergeField_ , _Comparison_ , _CompareTo_ , _TrueAutoText_ , _TrueText_ , _FalseAutoText_ , _FalseText_ )

expression Required. A variable that represents a 'MailMergeFields' collection.

Parameters

Name Required/Optional Data type Description
Range Required Range object The location for the IF field.
MergeField Required String The merge field name.
Comparison Required WdMailMergeComparison The operator used in the comparison.
CompareTo Optional Variant The text to compare with the contents of MergeField.
TrueAutoText Optional Variant The AutoText entry that's inserted if the comparison is true. If this argument is specified, TrueText is ignored.
TrueText Optional Variant The text that's inserted if the comparison is true.
FalseAutoText Optional Variant The AutoText entry that's inserted if the comparison is false. If this argument is specified, FalseText is ignored.
FalseText Optional Variant The text that's inserted if the comparison is false.

Return value

MailMergeField

Remarks

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.

Example

This example inserts "for your personal use" if the Company merge field is blank and "for your business" if the Company merge field is not blank.

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

See also

MailMergeFields Collection Object

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.