Nevron Open Vision Documentation
Rich Text Editor / Mail Merge / Mail Merge Overview
In This Topic
    Mail Merge Overview
    In This Topic

    Mail Merge is a feature provided by advanced text editors that lets the user create multiple messages based on an input template and a data source. The template contains special placeholders called mail merge fields. The mail merge process creates a message from each data record by filling the mail merge fields with the corresponding values from the data record. This results in a number of messages equal to the number of records in the data source.

     Mail Merge Settings

    NOV Text Editor provides full support for generating mail merge messages and a wide range of data sources. The information necessary for the mail merge features (data source, field mappings, current data record index, etc.) is stored in an NMailMerge object. You can obtain and modify it through the MailMerge property of the rich text document's document block. The NMailMerge class contains the following properties:

    • DataSource - specifies the data source the mail merge should get data from.
    • PreviewMailMerge - determines whether the mail merge is in preview or in design mode. When set to true (i.e. the mail merge is in preview mode), the mail merge field values are populated with the data from the currently selected data record.
    • CurrentDataRecordIndex - gets/sets the currently selected record index. You can use this property to navigate through the data records.
    • DataRecordCount - gets the number of data records in the mail merge's data source.

    The following piece of code demonstrates how to switch a rich text document's mail merge to preview mode:

    Switch mail merge to preview mode
    Copy Code
    NDocumentBlock documentBlock = richTextView.Content;
    documentBlock.MailMerge.PreviewMailMerge = true;
     Merge to Single Document

    If you want to merge each record with the mail merge template and save the result to a separate page of one large document, you can use the NMergeAndSaveBatch like shown in the following code snippet:

    Merge to single document
    Copy Code
    NMergeAndSaveBatch batch = new NMergeAndSaveBatch();

    The batch will show a dialog to let the user select a target file for the merged document and will then perform the mail merge and save the result to that file. Each merged message will be placed on a separate page in the resulting document.

     Merge to Multiple Documents 

    If you want to merge each record with the mail merge template and save the result to a different file, you perform the mail merge manually by navigating to each data record and saving the resulting merged document. Note that if you don't want this navigation to affect the original document, you should clone the rich text view.

    The following code example demonstrates how to perform a manual mail merge and save each merged document to a file in a given target folder:

    Merge to multiple documents
    Copy Code
    // Clone the rich text view
    NRichTextView clonedRichTextView = (NRichTextView)m_RichText.DeepClone();
    // Switch the mail merge of the cloned rich text view to preview mode
    NMailMerge mailMerge = clonedRichTextView.Content.MailMerge;
    mailMerge.PreviewMailMerge = true;
    // Loop through all mail merge records to save individual documents to file
    for (int i = 0; i < mailMerge.DataRecordCount; i++)
        // Move to the next data source record
        mailMerge.CurrentDataRecordIndex = i;
        // Save the merged document to file
        string fileName = "Document" + i.ToString(CultureInfo.InvariantCulture) + ".docx";
        clonedRichTextView.SaveToFile(NPath.Combine(targetPath, fileName));
    See Also