Conditional fields during mail merge in C# and VB.NET

GemBox.Document supports conditional mail merging with conditional fields (IF fields) in Word documents. You can handle this action programmatically in C# and VB.NET.

When using IF fields, you can compare two text values in the mail merge process and merge or insert the appropriate result text.

The following example shows how to perform a conditional mail merge and get resolved IF field results.

Word document with MERGEFIELD and IF fields
Screenshot of Word file with MERGEFIELD and IF fields
Word document generated from conditional mail merge process
Screenshot of Word file created with conditional mail merge
Upload your file (Drag file here)
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        var document = DocumentModel.Load("%InputFileName%");

        var data = new
            FirstName = "John",
            LastName = "Doe",
            Gender = "Male",
            Age = 30


        document.Save("Merged If Fields Output.%OutputFileType%");
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim document = DocumentModel.Load("%InputFileName%")

        Dim data = New With
            .FirstName = "John",
            .LastName = "Doe",
            .Gender = "Male",
            .Age = 30


        document.Save("Merged If Fields Output.%OutputFileType%")

    End Sub
End Module

The IF field's code uses the following syntax:

{ IF "Value1" Operator "Value2" "TrueText" "FalseText" }

Each part, except Operator, can contain text and other Field elements. After executing a mail merge process, the result is either TrueText or FalseText, based on the used comparison operator.

<>Not equal
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.