MailMerge

Document.MailMerge property

Returns a MailMerge object that represents the mail merge functionality for the document.

public MailMerge MailMerge { get; }

Examples

Shows how to execute a mail merge with data from a DataTable.

DataTable table = new DataTable("Test");
table.Columns.Add("CustomerName");
table.Columns.Add("Address");
table.Rows.Add(new object[] { "Thomas Hardy", "120 Hanover Sq., London" });
table.Rows.Add(new object[] { "Paolo Accorti", "Via Monte Bianco 34, Torino" });

// Below are two ways of using a DataTable as the data source for a mail merge.
// 1 -  Use the entire table for the mail merge to create one output mail merge document for every row in the table:
Document doc = CreateSourceDocExecuteDataTable();

doc.MailMerge.Execute(table);

doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.WholeTable.docx");

// 2 -  Use one row of the table to create one output mail merge document:
doc = CreateSourceDocExecuteDataTable();

doc.MailMerge.Execute(table.Rows[1]);

doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.OneRow.docx");

Shows how to execute a mail merge with data from a DataTable (CreateSourceDocExecuteDataTable).

/// <summary>
/// Creates a mail merge source document.
/// </summary>
private static Document CreateSourceDocExecuteDataTable()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.InsertField(" MERGEFIELD CustomerName ");
    builder.InsertParagraph();
    builder.InsertField(" MERGEFIELD Address ");

    return doc;
}

See Also