MailMergeUseWholeParagraphAsRegion Property |
Namespace: Aspose.Words.MailMerging
public void UseWholeParagraphAsRegion() { // Create a document with 2 mail merge regions in one paragraph and a table to which can fill one of the regions during a mail merge Document doc = CreateSourceDocWithNestedMergeRegions(); DataTable dataTable = CreateSourceTableDataTableForOneRegion(); // By default, a paragraph can belong to no more than one mail merge region // Our document breaks this rule so executing a mail merge with regions now will cause an exception to be thrown Assert.True(doc.MailMerge.UseWholeParagraphAsRegion); // If we set this variable to false, paragraphs and mail merge regions are independent so we can safely run our mail merge doc.MailMerge.UseWholeParagraphAsRegion = false; doc.MailMerge.ExecuteWithRegions(dataTable); // Our first region is populated, while our second is safely displayed as unused all across one paragraph doc.Save(ArtifactsDir + "MailMerge.UseWholeParagraphAsRegion.docx"); } /// <summary> /// Create a document with two mail merge regions sharing one paragraph. /// </summary> private static Document CreateSourceDocWithNestedMergeRegions() { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Region 1: "); builder.InsertField(" MERGEFIELD TableStart:MyTable"); builder.InsertField(" MERGEFIELD Column1"); builder.Write(", "); builder.InsertField(" MERGEFIELD Column2"); builder.InsertField(" MERGEFIELD TableEnd:MyTable"); builder.Write(", Region 2: "); builder.InsertField(" MERGEFIELD TableStart:MyOtherTable"); builder.InsertField(" MERGEFIELD TableEnd:MyOtherTable"); return doc; } /// <summary> /// Create a data table that can populate one region during a mail merge. /// </summary> private static DataTable CreateSourceTableDataTableForOneRegion() { DataTable dataTable = new DataTable("MyTable"); dataTable.Columns.Add("Column1"); dataTable.Columns.Add("Column2"); dataTable.Rows.Add(new object[] { "Value 1", "Value 2" }); return dataTable; }