After you have the template properly prepared, you are ready to run mail merge. Use the MailMerge object methods to execute it. The MailMerge object is returned by the Document.MailMerge property.
Call MailMerge.Execute passing it a data source object to perform a simple mail merge. Here is a list of the data objects acceptable by the MailMerge.Execute overloads:
- DataTable. Fills mail merge fields in the document with values from a DataTable.
- DataView. This method is useful if you retrieve data into a DataTable but then need to apply a filter or sort before the mail merge.
- DataRow. Fills mail merge fields in the document with values from a single DataRow.
- IDataReader. You can pass SqlDataReader or OleDbDataReader object into this method as a parameter because they both implement the IDataReader interface.
- A pair of arrays, one of which represents a set of the field names (array of strings), and another that represents a set of the corresponding field values (array of objects). Note that the number of array elements must be the same in both of the arrays.
Field names are not case sensitive. If a field name is not found in the document but is encountered in the data source, it is ignored.
Let us take an example. Imagine that you need to create a personalized letter filled with the data entered by the user in your application. You prepare the template accordingly by inserting merge fields named Company, Address, Address2, and so on. Then you create two arrays and pass them to MailMerge.Execute.
Example MailMergeArray
Performs a simple insertion of data into merge fields and sends the document to the browser.
[C#]
// Open an existing document.
Document doc = new Document(MyDir + "MailMerge.ExecuteArray.doc");
// Fill the fields in the document with user data.
doc.MailMerge.Execute(
new string[] {"FullName", "Company", "Address", "Address2", "City"},
new object[] {"James Bond", "MI5 Headquarters", "Milbank", "", "London"});
// Send the document in Word format to the client browser.
doc.Save(
"PersonalizedLetter Out.doc",
SaveFormat.Doc,
SaveType.OpenInBrowser,
Response);
[Visual Basic]
' Open an existing document.
Dim doc As Document = New Document(MyDir & "MailMerge.ExecuteArray.doc")
' Fill the fields in the document with user data.
doc.MailMerge.Execute(New String() {"FullName", "Company", "Address", "Address2", "City"}, New Object() {"James Bond", "MI5 Headquarters", "Milbank", "", "London"})
' Send the document in Word format to the client browser.
doc.Save("PersonalizedLetter Out.doc", SaveFormat.Doc, SaveType.OpenInBrowser, Response)
[Java]
// Open an existing document.
Document doc = new Document(getMyDir() + "MailMerge.ExecuteArray.doc");
// Fill the fields in the document with user data.
doc.getMailMerge().execute(
new String[] {"FullName", "Company", "Address", "Address2", "City"},
new Object[] {"James Bond", "MI5 Headquarters", "Milbank", "", "London"});
// Save the document in Word format.
doc.save(getMyDir() + "PersonalizedLetter Out.doc", SaveFormat.DOC);