Articles in this section
Category / Section

How to perform mail merge using Dictionary as data source

2 mins read

Mail merge is a process of merging data from a data source to a Word template document. Syncfusion® Essential® DocIO is a .NET Word library used to generate reports like invoice, payroll, letter, etc., by performing mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can execute mail merge using Dictionary as IEnumerable collection.

Mail merge using Dictionary as data source

Essential® DocIO supports executing mail merge using IEnumerable collection through Execute API. You can pass Dictionary as IEnumerable collection by adding into a List collection of object type.

The following code example shows how to execute mail merge using Dictionary as IEnumerable collection.
C#

//Loads input document
WordDocument document = new WordDocument(“Template.docx”);
//Gets data as Dictionary collection to perform mail merge 
Dictionary<string, object> keyValuePairs = GetDetails();
//Creates “IEnumerable” collection using Dictionary
List<object> enumerableCollection = new List<object>();
enumerableCollection.Add(keyValuePairs);
//Performs mail merge
document.MailMerge.Execute(enumerableCollection);
//Saves and close the document
document.Save("Sample.docx");
document.Close();

VB

'Loads input document
Dim document As WordDocument = New WordDocument(“Template.docx”)
'Gets data using Dictionary
Dim keyValuePairs As Dictionary(Of String, Object) = GetDetails
'Initialize the list of object
Dim enumerableCollection As List(Of Object) = New List(Of Object)
'Adds the Dictionary to the list
enumerableCollection.Add(keyValuePairs)
'Execute mail merge function with IEnumerable collection
document.MailMerge.Execute(enumerableCollection)
'Saves and close the document
document.Save("Sample.docx")
document.Close

The following code example shows GetDetails method which is used to get the data for mail merge.

C#

public Dictionary<string, object> GetDetails()
{
    //Creates a Dictionary collection to maintain field names as keys and field values as Values 
    Dictionary<string, object> stringList = new Dictionary<string, object>();
    //Adds the keys and values to the Dictonary
    stringList.Add("Cycle", "Cycle");
    stringList.Add("Vehicle", System.Drawing.Image.FromFile(“Mountain-200.jpg”));
    stringList.Add("Purchase Date", new DateTime(2019, 8, 1));
    stringList.Add("Purchase Price", "3500");
    stringList.Add("Purchase Mileage", "50");
    stringList.Add("Sold Date", new DateTime(2019, 8, 9));
    stringList.Add("Sold Price", "5000");
    stringList.Add("Sold Mileage", "50");
    stringList.Add("Color", "Blue");
    stringList.Add("Product Number", "BK-M68B-38");
    stringList.Add("VIN Number", "1234");
 
    return stringList;
}

VB

Public Function GetDetails() As Dictionary(Of String, Object)
' Creates a Dictionary collection to maintain field names as keys and field values as Values Dim stringList As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
'Adds the keys and values to the Dictionary
stringList.Add("Cycle", "Cycle")
stringList.Add("Vehicle", System.Drawing.Image.FromFile(“Mountain-200.jpg”))stringList.Add("Purchase Date", New DateTime(2019, 8, 1))
stringList.Add("Purchase Price", "3500")
stringList.Add("Purchase Mileage", "50")
stringList.Add("Sold Date", New DateTime(2019, 8, 9))
stringList.Add("Sold Price", "5000")
stringList.Add("Sold Mileage", "50")
stringList.Add("Color", "Blue")
stringList.Add("Product Number", "BK-M68B-38")
stringList.Add("VIN Number", "1234")
Return stringList
End Function
 

 

A complete working example of how to perform Mail merge using Dictionary as data source in Word document in C# can be downloaded from Mail merge using Dictionary.zip.

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied