AddIf Method

Microsoft Word Visual Basic

Adds an IF field to a mail merge main document. Returns a MailMergeField object. When updated, an IF field compares a field in a data record with a specified value, and then it inserts the appropriate text according to the result of the comparison.

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

expression    Required. An expression that returns a MailMergeFields object.

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.

WdMailMergeComparison can be one of these WdMailMergeComparison constants.
wdMergeIfEqual
wdMergeIfGreaterThanOrEqual
wdMergeIfIsNotBlank
wdMergeIfLessThanOrEqual
wdMergeIfGreaterThan
wdMergeIfIsBlank
wdMergeIfLessThan
wdMergeIfNotEqual

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.

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"