Skip to main content

RichEditControl.MailMerge(MailMergeOptions, String, DocumentFormat) Method

Merges data to the current document, and saves the result to a file with the specified format. Allows you to set mail merge options.

Namespace: DevExpress.XtraRichEdit

Assembly: DevExpress.XtraRichEdit.v24.1.dll

NuGet Packages: DevExpress.Win.PivotGrid, DevExpress.Win.RichEdit, DevExpress.Win.TreeMap

Declaration

public void MailMerge(
    MailMergeOptions options,
    string fileName,
    DocumentFormat format
)

Parameters

Name Type Description
options MailMergeOptions

An object that contains options for the current merge.

fileName String

A name of the file to which the merged document is saved.

format DocumentFormat

An enumeration member that indicates the destination format.

Remarks

The MailMerge method merges the current document with records retrieved from the MailMergeOptions.DataSource property specified by the options parameter, and saves the merged document to a file.

The MailMerge method results in the RichEditControl.CalculateDocumentVariable event being fired for the RichEditControl object.

Example

The following code creates a data source and runs the mail merge. The MailMergeOptions object is created in code, used to specify data rows and merge mode and is subsequently passed to the RichEditControl.MailMerge method. The current RichEditControl instance is passed to the BarItem.ItemClick event handler using the BarItem.Tag property.

View Example

using DevExpress.XtraRichEdit;
using System.Data;
using System.Diagnostics;

static void buttonCustomAction_ItemClick_MailMergeMethod(object sender, ItemClickEventArgs e) {

    RichEditControl richEdit = e.Item.Tag as RichEditControl;
    richEdit.LoadDocument("Documents\\MailMergeSimple.rtf", DocumentFormat.Rtf);
    DataTable MailMergeDataSource = new System.Data.DataTable();
    MailMergeDataSource.Columns.Add("FirstName");
    MailMergeDataSource.Columns.Add("LastName");
    MailMergeDataSource.Columns.Add("HiringDate");
    MailMergeDataSource.Columns.Add("Address");
    MailMergeDataSource.Columns.Add("City");
    MailMergeDataSource.Columns.Add("Country");
    MailMergeDataSource.Columns.Add("Position");
    MailMergeDataSource.Columns.Add("CompanyName");
    MailMergeDataSource.Columns.Add("Gender");
    MailMergeDataSource.Columns.Add("Phone");
    MailMergeDataSource.Columns.Add("HRManagerName");

    string[] firstName = { "Nancy", "Andrew", "Janet", "Margaret", 
                             "Steven", "Michael", "Robert", "Laura", "Anne" };

    string[] lastName = { "Davolio", "Fuller", "Leverling", "Peacock", 
                            "Buchanan", "Suyama", "King", "Callahan", "Dodsworth" };

    string[] city = { "Seattle", "Tacoma", "Kirkland", "Redmond", "London",                                 
                        "London", "London", "Seattle", "London" };

    string[] country = { "USA", "USA", "USA", "USA", 
                           "UK", "UK", "UK", "USA", "UK" };

    string[] address = { "507 - 20th Ave. E. Apt. 2A", "908 W. Capital Way", "722 Moss Bay Blvd.", 
                           "4110 Old Redmond Rd.", "14 Garrett Hill", "Coventry House Miner Rd.", 
                           "Edgeham Hollow Winchester Way", "4726 - 11th Ave. N.E.", "7 Houndstooth Rd." };

    string[] position = { "Sales Representative", "Vice President, Sales", "Sales Representative", 
                            "Sales Representative", "Sales Manager", "Sales Representative", 
                            "Sales Representative", "Inside Sales Coordinator", "Sales Representative" };

    char[] gender = { 'F', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'F' };
    string[] phone = { "(206) 555-9857", "(206) 555-9482", "(206) 555-3412", "(206) 555-8122", 
                         "(71) 555-4848", "(71) 555-7773", "(71) 555-5598", "(206) 555-1189", "(71) 555-4444" };

    string[] companyName = { "Consolidated Holdings", "Around the Horn", "North/South", "Island Trading", 
                               "White Clover Markets", "Trail's Head Gourmet Provisioners", "The Cracker Box", 
                               "The Big Cheese", "Rattlesnake Canyon Grocery", "Split Rail Beer & Ale", 
                               "Hungry Coyote Import Store", "Great Lakes Food Market" };

    Random rnd = new Random();
    for(int i = 0; i < 9; i++) {
        MailMergeDataSource.Rows.Add(new object[] { 
            firstName[i], 
            lastName[i], 
            DateTime.Now.AddDays(-(rnd.Next(0, 2000))), 
            address[i], 
            city[i], 
            country[i], 
            position[i], 
            companyName[i], 
            gender[i], 
            phone[i], 
            "Dan Marino" });
    }

    MailMergeOptions options = richEdit.CreateMailMergeOptions();
    options.DataSource = MailMergeDataSource;
    options.FirstRecordIndex = 2;
    options.LastRecordIndex = 5;
    options.MergeMode = MergeMode.NewSection;

    using(FileStream fsResult = new FileStream("MailMergeResult.rtf", FileMode.OpenOrCreate)) {

        richEdit.MailMerge(options, fsResult, DocumentFormat.Rtf);
    }
    Process.Start("MailMergeResult.rtf");
}
See Also