AddIf Method

Applies To

MailMergeFields collection object.

Description

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.

Syntax

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 Long. The operator used in the comparison. Can be one of the following WdMailMergeComparison constants: wdMergeIfEqual, wdMergeIfGreaterThan, wdMergeIfGreaterThanOrEqual, wdMergeIfIsBlank, wdMergeIfIsNotBlank, wdMergeIfLessThan, wdMergeIfLessThanOrEqual, or 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.

See Also

AddNext method, AddSkipIf method.

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"
This example inserts an IF field that compares the contents of the merge field named "Title" with the text "Mr." When the merge is performed, "Hello" is inserted if the comparison is true.

ActiveWindow.View.ShowFieldCodes = False
Set myRange = ActiveDocument.Range(Start:=0, End:=0)
With ActiveDocument.MailMerge.Fields
    .AddIf Range:=myRange, MergeField:="Title", _
        Comparison:=wdMergeIfEqual, CompareTo:="Mr.", TrueText:="Hello "
End With