Simple Mail merge in Word document
5 Sep 20246 minutes to read
You can create a Word document template using Microsoft Word application or by adding merge fields in the Word document programmatically. For further information, click here.
The following table illustrates the supported mail merge overloads for Execute method.
Overloads |
Examples |
---|---|
Mail merge with string arrays
The MailMerge class provides various overloads for Execute method to perform Mail merge from various data sources. The Mail merge operation replaces the matching merge fields with the respective data.
Create Word document template
The following code example shows how to create a Word template document with merge fields.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Creates an instance of a WordDocument
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.EnsureMinimal();
//Sets page margins to the last section of the document
document.LastSection.PageSetup.Margins.All = 72;
//Appends text to the last paragraph.
document.LastParagraph.AppendText("EmployeeId: ");
//Appends merge field to the last paragraph.
document.LastParagraph.AppendField("EmployeeId", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nName: ");
document.LastParagraph.AppendField("Name", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nPhone: ");
document.LastParagraph.AppendField("Phone", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nCity: ");
document.LastParagraph.AppendField("City", FieldType.FieldMergeField);
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document
document.Close();
//Creates an instance of a WordDocument
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.EnsureMinimal();
//Sets page margins to the last section of the document
document.LastSection.PageSetup.Margins.All = 72;
//Appends text to the last paragraph.
document.LastParagraph.AppendText("EmployeeId: ");
//Appends merge field to the last paragraph.
document.LastParagraph.AppendField("EmployeeId", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nName: ");
document.LastParagraph.AppendField("Name", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nPhone: ");
document.LastParagraph.AppendField("Phone", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nCity: ");
document.LastParagraph.AppendField("City", FieldType.FieldMergeField);
//Saves and closes the WordDocument instance.
document.Save("Template.docx", FormatType.Docx);
document.Close();
'Creates an instance of a WordDocument
Dim document As New WordDocument()
'Adds one section and one paragraph to the document
document.EnsureMinimal()
'Sets page margins to the last section of the document
document.LastSection.PageSetup.Margins.All = 72
'Appends text to the last paragraph.
document.LastParagraph.AppendText("EmployeeId: ")
'Appends merge field to the last paragraph.
document.LastParagraph.AppendField("EmployeeId", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "Name: ")
document.LastParagraph.AppendField("Name", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "Phone: ")
document.LastParagraph.AppendField("Phone", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "City: ")
document.LastParagraph.AppendField("City", FieldType.FieldMergeField)
'Saves and closes the WordDocument instance.
document.Save("Template.docx", FormatType.Docx)
document.Close()
You can download a complete working sample from GitHub.
The generated template document looks as follows.
Execute mail merge
The following code example shows how to perform a simple Mail merge in the generated template document with string array as data source.
//Opens the template document
FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
string[] fieldNames = new string[] { "EmployeeId", "Name", "Phone", "City" };
string[] fieldValues = new string[] { "1001", "Peter", "+122-2222222", "London" };
//Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document
document.Close();
stream.Position = 0;
//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Opens the template document
WordDocument document = new WordDocument("Template.docx");
string[] fieldNames = new string[] { "EmployeeId", "Name", "Phone", "City" };
string[] fieldValues = new string[] { "1001", "Peter", "+122-2222222", "London" };
//Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
//Saves and closes the WordDocument instance
document.Save("Sample.docx", FormatType.Docx);
document.Close();
'Opens the template document
Dim document As New WordDocument("Template.docx")
Dim fieldNames As String() = New String() {"EmployeeId", "Name", "Phone", "City"}
Dim fieldValues As String() = New String() {"1001", "Peter", "+122-2222222", "London"}
'Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues)
'Saves and closes the WordDocument instance
document.Save("Sample.docx", FormatType.Docx)
document.Close()
You can download a complete working sample from GitHub.
The resultant document looks as follows.