How to Realize Word Mail Merge Function with Silverlight

Mail Merge is a powerful function in Microsoft Word. It connect template with data source to create several items with different data information. Therefore, it is very convenient for users to print the item in batch or send via Email at one time.

Before using mail merge, we need to prepare two elements, templates and data source. Template can be taken as a framework, which includes cue words and contents that can be used in each item. Data source can be data from database or Excel.

In this post, I will introduce a method to realize Word mail merge with Silverlight.

I prepare a template which is about mail. This template includes receiver and sender name, receiver Email address, date, subject and mail contents. Also, I set format for this template to make its appearance be more wonderful.

Because this is a simple example, so I don’t get data source from Database or Excel, but import data information manually in code.

The following screenshot presents my template contents.

Please note that: I use a Silverlight Word Component, Spire.Doc for Silverlight in this example, so I have added its dll file as reference in my project at the beginning.


Step 1. Declare SaveFileDialog

For saving the result document, I need to declare a SaveFileDialog and set filter for it. The filter is set as Word document with extension .docx because my template file is .docx. Also, it can be set as .doc as well.

Step 2. Load Document

Right click project name to add existing item (template) and change the template Build Action as embedded resource after adding.

Then, add a button in UserControl. Double click it to write code. Firstly, declare document and assembly. Use foreach sentence to get file name from assembly. If the file name is the same as embedded resource name, load this document.

Step 3. Mail Merge

Because data information will be imported to fields which are set in template, so we need to get the Names and Values string of fields at first. After getting the string array, use document.MailMerge.Execute(fieldNames, fieldValues) method to realize mail merge.

Step 4. Save Document

Judge if the SaveFileDialog can pop up. If the result is true, save this document by using document.SaveToStream(stream, FileFormat) method.

Full DocxMailMerge.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using Spire.Doc;


namespace SLProjects


    public partial class MainPage : UserControl


        //Declare SaveFileDialog

        private SaveFileDialog saveFileDialog = new SaveFileDialog();

        public MainPage()



            this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;



        private void button1_Click(object sender, RoutedEventArgs e)


            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())


                if (name.EndsWith(“Mail.docx”))


                    using (Stream docStream = assembly.GetManifestResourceStream(name))


                        document = new Document(docStream, FileFormat.Docx);





            //Mail Merge

            string[] fieldNames = new string[] {“Contact_Name”, “Email”,“Sender_Name”,“Date”, “Subject” };

            string[] fieldValues = new string[] { “Robert”, “”, “Janey”,System.DateTime.Now.Date.ToString(),“Away on Official Business” };

            document.MailMerge.Execute(fieldNames, fieldValues);


            //Save Document

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)


                using (Stream stream = this.saveFileDialog.OpenFile())


                    document.SaveToStream(stream, FileFormat.Docx);









Freely Download Silverlight Word Component


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s