Mail merging Word documents in ASP.NET MVC
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 mail merge Word documents in ASP.NET MVC.
Steps to Mail merge Word Document Programmatically:
- Create a new ASP.NET MVC application project.

- Install the Syncfusion.DocIO.AspNet.Mvc5 NuGet package as a reference to your project from NuGet.org.

- Include the following namespaces in the HomeController.cs file:
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS
- A default action method named Index will be present in HomeController.cs. Right-click on this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
- Add a new button in Index.cshtml as shown below:
CSHTML
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Create Document" style="width:150px;height:27px" />
</div>
}
Html.EndForm();
}
- Add a new action method CreateDocument in HomeController.cs and include the code below to mail merge the Word document and download it.
C#
// Opens the Word template document
using (WordDocument document = new WordDocument(ResolveApplicationDataPath("/") + "/Letter Formatting.docx"))
{
string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };
string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" };
// Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
// Saves the Word document to disk in DOCX format
document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment);
}
// Get the path for the Word template document
protected string ResolveApplicationDataPath(string fileName)
{
// Data folder path is resolved from the requested page physical path.
string dataPath = new System.IO.DirectoryInfo(Request.PhysicalPath + "..\\..\\Data\\").FullName;
return string.Format("{0}\\{1}", dataPath, fileName);
}
VB
'Opens the Word template document
Using document As WordDocument = New WordDocument(ResolveApplicationDataPath("/") + "/Letter Formatting.docx")
Dim fieldNames As String() = {"ContactName", "CompanyName", "Address", "City", "Country", "Phone" }
Dim fieldValues As String() = {"Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" }
'Performs the mail merge
document.MailMerge.Execute(fieldNames, fieldValues)
'Saves the Word document to disk in DOCX format
document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)
End Using
'Get the path for the Word template document
Protected Function ResolveApplicationDataPath(fileName As String) As String
Dim dataPath As String = New System.IO.DirectoryInfo(Request.PhysicalPath + "..\Data\").FullName
Return String.Format("{0}\{1}", dataPath, fileName)
End Function
A complete working example of mail merge Word document in ASP.NET MVC can be downloaded from mail merge Word document.zip
By executing the program, you will get the Word document as follows: 
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.
Explore more about the rich set of Syncfusion® Word Framework features.
An online example to perform mail merge in a Word document.
See Also:
Mail merge Word document in ASP.NET
Mail merge Word document in ASP.NET Core
Mail merge Word document in WPF
Mail merge Word document in Windows Forms
Mail merge Word document in UWP
Mail merge Word document in Xamarin
Mail merge Word document in Xamarin.Android
Mail merge Word document in Xamarin.iOS
Mail merge Word document in C#
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to mail merge Word document in ASP.NET MVC.
You can refer to our ASP.NET MVC Word feature tour page to learn about its other groundbreaking feature representations. You can also explore our ASP.NET MVC Word examples to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET MVC components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our ASP.NET MVC PowerPoint and other ASP.NET MVC components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!