MailMergeUseWholeParagraphAsRegion Property

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.

Namespace:  Aspose.Words.MailMerging
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.9.0

Syntax

public bool UseWholeParagraphAsRegion { get; set; }

Property Value

Type: Boolean

Remarks

The default value is true.

Examples

Shows the relationship between mail merge regions and paragraphs.
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);
    Assert.Throws<InvalidOperationException>(() => doc.MailMerge.ExecuteWithRegions(dataTable));

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

See Also