MailMergeMergeDuplicateRegions Property

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.

Namespace:  Aspose.Words.MailMerging
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public bool MergeDuplicateRegions { get; set; }

Property Value

Type: Boolean
Remarks
The default value is false.
Examples
Shows how to work with duplicate mail merge regions.
public void MergeDuplicateRegions(bool isMergeDuplicateRegions)
{
    // Create a document and table that we will merge
    Document doc = CreateSourceDocMergeDuplicateRegions();
    DataTable dataTable = CreateSourceTableMergeDuplicateRegions();

    // If this property is false, the first region will be merged
    // while the MERGEFIELDs of the second one will be left in the pre-merge state
    // To get both regions merged we would have to execute the mail merge twice, on a table of the same name
    // If this is set to true, both regions will be affected by the merge
    doc.MailMerge.MergeDuplicateRegions = isMergeDuplicateRegions;

    doc.MailMerge.ExecuteWithRegions(dataTable);
    doc.Save(ArtifactsDir + "MailMerge.MergeDuplicateRegions.docx");
}

/// <summary>
/// Return a document that contains two duplicate mail merge regions (sharing the same name in the "TableStart/End" tags).
/// </summary>
private static Document CreateSourceDocMergeDuplicateRegions()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.InsertField(" MERGEFIELD TableStart:MergeRegion");
    builder.InsertField(" MERGEFIELD Column1");
    builder.InsertField(" MERGEFIELD TableEnd:MergeRegion");
    builder.InsertParagraph();

    builder.InsertField(" MERGEFIELD TableStart:MergeRegion");
    builder.InsertField(" MERGEFIELD Column2");
    builder.InsertField(" MERGEFIELD TableEnd:MergeRegion");

    return doc;
}

/// <summary>
/// Create a data table with one row and two columns.
/// </summary>
private static DataTable CreateSourceTableMergeDuplicateRegions()
{
    DataTable dataTable = new DataTable("MergeRegion");
    dataTable.Columns.Add("Column1");
    dataTable.Columns.Add("Column2");
    dataTable.Rows.Add(new object[] { "Value 1", "Value 2" });

    return dataTable;
}
See Also