Merge Ranges

With GemBox.Document you can perform mail merging on a whole document or parts of the document (on so called named merge ranges).

To define a merge range you need a pair of MERGEFIELD elements; one should have MailMerge.RangeStartPrefix and the other should have MailMerge.RangeEndPrefix in its name.

When you're providing a sequence as a data source (like IEnumerable with multiple items or DataTable with multiple rows) the merge range will be duplicated for each record and merged with the record's data.

The following example shows how you can perform a mail merge on a named merge range using MERGEFIELD elements with a default "RangeStart:" and "RangeEnd:" prefix.

Word document generated from merging named range
Screenshot of Word file created with merged range
Upload your file (Drag file here)
using System;
using System.Data;
using System.Linq;
using GemBox.Document;

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

        int numberOfItems = %NumberOfItems%;

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

        // Create DataTable as a data source for merge range.
        var table = new DataTable() { TableName = "Items" };

        table.Columns.Add("Date", typeof(DateTime));
        table.Columns.Add("Hours", typeof(int));
        table.Columns.Add("Unit", typeof(double));
        table.Columns.Add("Price", typeof(double));

        for (int rowIndex = 1; rowIndex <= numberOfItems; rowIndex++)
            DateTime date = DateTime.Today.AddDays(rowIndex - numberOfItems);
            int hours = rowIndex % 3 + 6;
            double unit = 35.0;
            double price = hours * unit;

            table.Rows.Add(date, hours, unit, price);

        // Execute mail merge process for "Items" merge range.

        // Execute mail merge process again for "Number", "Date" and "TotalPrice" fields.
                Number = 10203,
                Date = DateTime.Now,
                TotalPrice = table.Rows.Cast<DataRow>().Sum(row => (double)row["Price"])

        document.Save("Merged Ranges Output.%OutputFileType%");
Imports System
Imports System.Data
Imports System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim numberOfItems As Integer = %NumberOfItems%

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

        ' Create DataTable as a data source for merge range.
        Dim table As New DataTable() With {.TableName = "Items"}

        table.Columns.Add("Date", GetType(DateTime))
        table.Columns.Add("Hours", GetType(Integer))
        table.Columns.Add("Unit", GetType(Double))
        table.Columns.Add("Price", GetType(Double))

        For rowIndex As Integer = 1 To numberOfItems

            Dim [date] As DateTime = DateTime.Today.AddDays(rowIndex - numberOfItems)
            Dim hours As Integer = rowIndex Mod 3 + 6
            Dim unit As Double = 35.0
            Dim price As Double = hours * unit

            table.Rows.Add([date], hours, unit, price)


        ' Execute mail merge process for "Items" merge range.

        ' Execute mail merge process again for "Number", "Date" and "TotalPrice" fields.
            New With
                .Number = 10203,
                .Date = DateTime.Now,
                .TotalPrice = table.Rows.Cast(Of DataRow)().Sum(Function(row) CDbl(row("Price")))

        document.Save("Merged Ranges Output.%OutputFileType%")

    End Sub
End Module

You may notice that the above example calls the MailMerge.Execute method multiple times. It is safe to chain mail merge operations (perform multiple successive mail merges) on the same document.

In that case, you'll probably want to specify the Clear Options only for the last mail merge operation.

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.