Support Center

Mail Merge remove empty table

When working with mail merge, one of the common uses is to execute a mail merge operation over the Table element in order to get a dynamic number of TableRows (depending on the data source records).

Demonstration sample of this feature can be found on the following link:
http://www.gemboxsoftware.com/SampleExplorer/Document/MailMerge/MergeRanges

Also, here is another sample which uses the following "Mail Merge Sample.docx" file as an input:

Mail Merge Sample.docx

The following code will perform a mail merge on "ITEMS" range and import the data from the data source.
Note that an input file contains range name prefixes different from the ones that GemBox.Document uses by default.

C# code

DocumentModel document = DocumentModel.Load("Mail Merge Sample.docx");

// Change the prefix that indicates range start, default is "RangeStart:".
document.MailMerge.RangeStartPrefix = "START:";
// Change the prefix that indicates range start, default is "RangeEnd:".
document.MailMerge.RangeEndPrefix = "END:";

// Create a data source.
var dataSource = new object[]
{
    new { ID = 1, FirstData = "Sample 1", SecondData = "Sample 1" },
    new { ID = 2, FirstData = "Sample 2", SecondData = "Sample 2" },
    new { ID = 3, FirstData = "Sample 3", SecondData = "Sample 3" },
    new { ID = 4, FirstData = "Sample 4", SecondData = "Sample 4" },
    new { ID = 5, FirstData = "Sample 5", SecondData = "Sample 5" },
};

document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges;
document.MailMerge.Execute(dataSource, "ITEMS");
document.Save("Sample Out Empty.docx");

VB.NET code

Dim document As DocumentModel = DocumentModel.Load("Mail Merge Sample.docx")

' Change the prefix that indicates range start, default is "RangeStart:".
document.MailMerge.RangeStartPrefix = "START:"
' Change the prefix that indicates range start, default is "RangeEnd:".
document.MailMerge.RangeEndPrefix = "END:"

' Create a data source.
Dim dataSource = New Object() _
{
    New With {.ID = 1, .FirstData = "Sample 1", .SecondData = "Sample 1"},
    New With {.ID = 1, .FirstData = "Sample 2", .SecondData = "Sample 2"},
    New With {.ID = 1, .FirstData = "Sample 3", .SecondData = "Sample 3"},
    New With {.ID = 1, .FirstData = "Sample 4", .SecondData = "Sample 4"},
    New With {.ID = 1, .FirstData = "Sample 5", .SecondData = "Sample 5"}
}

document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges
document.MailMerge.Execute(dataSource, "ITEMS")
document.Save("Sample Out Empty.docx")

The following is the resulting "Sample Out.docx" file:

Sample Out.docx

Data source values are successfully imported into a Table element, but if we were to use an empty data source or a data source with all the values equal to null or empty, the resulting file would be the following:

Sample Out Empty.docx

Due to the use of MailMergeClearOptions.RemoveEmptyRanges, the resulting output file will not contain the "ITEMS" range, but the Table element's header row still remains because it is not part of the range.
In order to remove the whole Table element for empty data sources, we will define another merge range "ItemsContainer" which will contain an existing "ITEMS" range.

The following is a "Mail Merge Sample 2.docx" file that will be used as an input:

Mail Merge Sample 2.docx

In order to use this new input file, we will have to adjust the data source used for a mail merge execution, as the following:

C# code

// Create a data source.
var dataSource = new
{
    ITEMS = new object[]
    {
        new { ID = 1, FirstData = "Sample 1", SecondData = "Sample 1" },
        new { ID = 2, FirstData = "Sample 2", SecondData = "Sample 2" },
        new { ID = 3, FirstData = "Sample 3", SecondData = "Sample 3" },
        new { ID = 4, FirstData = "Sample 4", SecondData = "Sample 4" },
        new { ID = 5, FirstData = "Sample 5", SecondData = "Sample 5" },
    }
};

document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges;
document.MailMerge.Execute(dataSource, "ItemsContainer");
document.Save("Sample Out Empty.docx");

VB.NET code

' Create a data source.
Dim dataSource = New With
{
    .ITEMS = New Object() _
    {
        New With {.ID = 1, .FirstData = "Sample 1", .SecondData = "Sample 1"},
        New With {.ID = 1, .FirstData = "Sample 2", .SecondData = "Sample 2"},
        New With {.ID = 1, .FirstData = "Sample 3", .SecondData = "Sample 3"},
        New With {.ID = 1, .FirstData = "Sample 4", .SecondData = "Sample 4"},
        New With {.ID = 1, .FirstData = "Sample 5", .SecondData = "Sample 5"}
    }
}

document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges
document.MailMerge.Execute(dataSource, "ItemsContainer")
document.Save("Sample Out Empty.docx")

But now if we were to use an empty data source, we would achieve the required behaviour of removing the entire Table element and the resulting output file would be as shown on the following image:

Sample Out Empty 2.docx

4 of 5 people found this page helpful
Subscribe to this article to get an email notification when it is updated.

0 Comments

  • There are no comments.