MergeWholeDocument

MailMerge.MergeWholeDocument property

Gets or sets a value indicating whether fields in whole document are updated while executing of a mail merge with regions.

public bool MergeWholeDocument { get; set; }

Remarks

The default value is false.

Examples

Shows the relationship between mail merges with regions, and field updating.

public void MergeWholeDocument(bool mergeWholeDocument)
{
    Document doc = CreateSourceDocMergeWholeDocument();
    DataTable dataTable = CreateSourceTableMergeWholeDocument();

    // If we set the "MergeWholeDocument" flag to "true",
    // the mail merge with regions will update every field in the document.
    // If we set the "MergeWholeDocument" flag to "false", the mail merge will only update fields
    // within the mail merge region whose name matches the name of the data source table.
    doc.MailMerge.MergeWholeDocument = mergeWholeDocument;
    doc.MailMerge.ExecuteWithRegions(dataTable);

    // The mail merge will only update the QUOTE field outside of the mail merge region
    // if we set the "MergeWholeDocument" flag to "true".
    doc.Save(ArtifactsDir + "MailMerge.MergeWholeDocument.docx");

    Assert.True(doc.GetText().Contains("This QUOTE field is inside the \"MyTable\" merge region."));
    Assert.AreEqual(mergeWholeDocument, 
        doc.GetText().Contains("This QUOTE field is outside of the \"MyTable\" merge region."));
}

/// <summary>
/// Create a document with a mail merge region that belongs to a data source named "MyTable".
/// Insert one QUOTE field inside this region, and one more outside it.
/// </summary>
private static Document CreateSourceDocMergeWholeDocument()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    FieldQuote field = (FieldQuote)builder.InsertField(FieldType.FieldQuote, true);
    field.Text = "This QUOTE field is outside of the \"MyTable\" merge region.";

    builder.InsertParagraph();
    builder.InsertField(" MERGEFIELD TableStart:MyTable");

    field = (FieldQuote)builder.InsertField(FieldType.FieldQuote, true);
    field.Text = "This QUOTE field is inside the \"MyTable\" merge region.";
    builder.InsertParagraph();

    builder.InsertField(" MERGEFIELD MyColumn");
    builder.InsertField(" MERGEFIELD TableEnd:MyTable");

    return doc;
}

/// <summary>
/// Create a data table that will be used in a mail merge.
/// </summary>
private static DataTable CreateSourceTableMergeWholeDocument()
{
    DataTable dataTable = new DataTable("MyTable");
    dataTable.Columns.Add("MyColumn");
    dataTable.Rows.Add(new object[] { "MyValue" });

    return dataTable;
}

See Also