MailMergePreserveUnusedTags Property

Gets or sets a value indicating whether the unused "mustache" tags should be preserved.

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

Syntax

public bool PreserveUnusedTags { get; set; }

Property Value

Type: Boolean

Remarks

The default value is false.

Examples

Shows how to preserve the appearance of alternative mail merge tags that go unused during a mail merge.
public void PreserveUnusedTags(bool doPreserveUnusedTags)
{
    // Create a document and table that we will merge
    Document doc = CreateSourceDocWithAlternativeMergeFields();
    DataTable dataTable = CreateSourceTablePreserveUnusedTags();

    // By default, alternative merge tags that cannot receive data because the data source has no columns with their name
    // are converted to and left on display as MERGEFIELDs after the mail merge
    // We can preserve their original appearance setting this attribute to true
    doc.MailMerge.PreserveUnusedTags = doPreserveUnusedTags;
    doc.MailMerge.Execute(dataTable);

    doc.Save(ArtifactsDir + "MailMerge.PreserveUnusedTags.docx");

    Assert.AreEqual(doc.GetText().Contains("{{ Column2 }}"), doPreserveUnusedTags);
}

/// <summary>
/// Create a document and add two tags that can accept mail merge data that are not the traditional MERGEFIELDs.
/// </summary>
private static Document CreateSourceDocWithAlternativeMergeFields()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.Writeln("{{ Column1 }}");
    builder.Writeln("{{ Column2 }}");

    // Our tags will only register as destinations for mail merge data if we set this to true
    doc.MailMerge.UseNonMergeFields = true;

    return doc;
}

/// <summary>
/// Create a simple data table with one column.
/// </summary>
private static DataTable CreateSourceTablePreserveUnusedTags()
{
    DataTable dataTable = new DataTable("MyTable");
    dataTable.Columns.Add("Column1");
    dataTable.Rows.Add(new object[] { "Value1" });

    return dataTable;
}

See Also