Mail Merge is very useful for generating template file in bulk, such as transcript, questionnaire, invoice and so on.

How to use mail merge in MS Office? Firstly, we need to create a template, which is used to generate frame and main contents of file. Then, we need to have data source. Generally speaking, we can export data from database or create a new Excel file with essential data information. Secondly, set mail merge type and link to data source. Finally, generate mail merge files and set format to be convenient for printing.

Then, I want to show an example about how to use Word mail merge with C#. This template is a fax, including sender and receiver name, fax number, date, subject and message.

In this method, a component: Spire.Doc is used, which specializes in managing MS Word with .NET and Silverlight. If you want to use the following code, please download and install it. Then, add DLL file in your project.

private void button1_Click(object sender, EventArgs e)

{

    //Create word document

    Document document = new Document();

    document.LoadFromFile(@"..\..\..\..\..\..\Data\Fax.doc");

 

    string[] filedNames = new string[]{"Contact Name","Fax","Date"};

 

    string[] filedValues = new string[]{"John Smith","+1 (69) 123456",System.DateTime.Now.Date.ToString()};

 

    document.MailMerge.Execute(filedNames, filedValues);

 

    //Save doc file.

    document.SaveToFile("Sample.doc", FileFormat.Doc);

 

    //Launching the MS Word file.

    WordDocViewer("Sample.doc");

}

 

private void WordDocViewer(string fileName)

{

    try

    {

        System.Diagnostics.Process.Start(fileName);

    }

    catch { }

}