MailMergeMergeDuplicateRegions Property |
Namespace: Aspose.Words.MailMerging
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; }