MailMerge

MailMerge class

Represents the mail merge functionality.

public class MailMerge

Properties

Name Description
CleanupOptions { get; set; } Gets or sets a set of flags that specify what items should be removed during mail merge.
CleanupParagraphsWithPunctuationMarks { get; set; } Gets or sets a value indicating whether paragraphs with punctuation marks are considered as empty and should be removed if the RemoveEmptyParagraphs option is specified.
FieldMergingCallback { get; set; } Occurs during mail merge when a mail merge field is encountered in the document.
MailMergeCallback { get; set; } Allows to handle particular events during mail merge.
MappedDataFields { get; } Returns a collection that represents mapped data fields for the mail merge operation.
MergeDuplicateRegions { get; set; } Gets or sets a value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.
MergeWholeDocument { get; set; } Gets or sets a value indicating whether fields in whole document are updated while executing of a mail merge with regions.
PreserveUnusedTags { get; set; } Gets or sets a value indicating whether the unused “mustache” tags should be preserved.
RegionEndTag { get; set; } Gets or sets a mail merge region end tag.
RegionStartTag { get; set; } Gets or sets a mail merge region start tag.
RestartListsAtEachSection { get; set; } Gets or sets a value indicating whether lists are restarted at each section after executing of a mail merge.
RetainFirstSectionStart { get; set; } Gets or sets a value indicating whether the SectionStart of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.
TrimWhitespaces { get; set; } Gets or sets a value indicating whether trailing and leading whitespaces are trimmed from mail merge values.
UnconditionalMergeFieldsAndRegions { get; set; } Gets or sets a value indicating whether merge fields and merge regions are merged regardless of the parent IF field’s condition.
UseNonMergeFields { get; set; } When true, specifies that in addition to MERGEFIELD fields, mail merge is performed into some other types of fields and also into “{{fieldName}}” tags.
UseWholeParagraphAsRegion { get; set; } Gets or sets a value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.

Methods

Name Description
DeleteFields() Removes mail merge related fields from the document.
Execute(DataRow) Performs mail merge from a DataRow into the document.
Execute(DataTable) Performs mail merge from a DataTable into the document.
Execute(DataView) Performs mail merge from a DataView into the document.
Execute(IDataReader) Performs mail merge from IDataReader into the document.
Execute(IMailMergeDataSource) Performs a mail merge from a custom data source.
Execute(string[], object[]) Performs a mail merge operation for a single record.
ExecuteADO(object) Performs mail merge from an ADO Recordset object into the document.
ExecuteWithRegions(DataSet) Performs mail merge from a DataSet into a document with mail merge regions.
ExecuteWithRegions(DataTable) Performs mail merge from a DataTable into the document with mail merge regions.
ExecuteWithRegions(DataView) Performs mail merge from a DataView into the document with mail merge regions.
ExecuteWithRegions(IMailMergeDataSource) Performs a mail merge from a custom data source with mail merge regions.
ExecuteWithRegions(IMailMergeDataSourceRoot) Performs a mail merge from a custom data source with mail merge regions.
ExecuteWithRegions(IDataReader, string) Performs mail merge from IDataReader into the document with mail merge regions.
ExecuteWithRegionsADO(object, string) Performs mail merge from an ADO Recordset object into the document with mail merge regions.
GetFieldNames() Returns a collection of mail merge field names available in the document.
GetFieldNamesForRegion(string) Returns a collection of mail merge field names available in the region.
GetFieldNamesForRegion(string, int) Returns a collection of mail merge field names available in the region.
GetRegionsByName(string) Returns a collection of mail merge regions with the specified name.
GetRegionsHierarchy() Returns a full hierarchy of regions (with fields) available in the document.

Remarks

For mail merge operation to work, the document should contain Word MERGEFIELD and optionally NEXT fields. During mail merge operation, merge fields in the document are replaced with values from your data source.

There are two distinct ways to use mail merge: with mail merge regions and without.

The simplest mail merge is without regions and it is very similar to how mail merge works in Word. Use Execute methods to merge information from some data source such as DataTable, DataSet, DataView, IDataReader or an array of objects into your document. The MailMerge object processes all records of the data source and copies and appends content of the whole document for each record.

Note that when MailMerge object encounters a NEXT field, it selects next record in the data source and continues merging without copying any content.

Use ExecuteWithRegions methods to merge information into a document with mail merge regions defined. You can use DataSet, DataTable, DataView or IDataReader as data sources for this operation.

You need to use mail merge regions if you want to dynamically grow portions inside the document. Without mail merge regions whole document will be repeated for every record of the data source.

Examples

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

public void ExecuteDataTable()
{
    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");
}

/// <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