Articles in this section
Category / Section

How to perform a mail merge for required fields in a Word document?

2 mins read

Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can perform a mail merge for the required fields in a Word document using C#.

If you wish to execute a mail merge for particular fields in the first phase and other fields in the next phase of execution, you can achieve this by setting the `ClearFields` API to false in the Word library (DocIO).

Steps to perform a mail merge for required fields in a Word document:

  1. Create a new C# .NET Core console application project.

Create .NET Core console application in Visual Studio

  1. Install the Syncfusion.DocIO.NET.Core NuGet package as a reference to your ASP.NET Core application from NuGet.org.

Add DocIO NuGet packages of ASP.NET Core

  1. Include the following namespaces in the Program.cs file:

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code snippet to perform a mail merge for required fields in a Word document:

C#

using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    // Open the input Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
    {
        // Disable the flag to maintain unmerged fields in the document for the next phase of execution.
        document.MailMerge.ClearFields = false;
 
        // First phase merge field execution.
        string[] phase1_FieldName = new string[] { "EmployeeId" };
        string[] phase1_FieldValue = new string[] { "1001" };
        // Perform the mail merge.
        document.MailMerge.Execute(phase1_FieldName, phase1_FieldValue);
 
        // Second phase merge fields execution.
        string[] phase2_FieldNames = new string[] { "Name", "Phone", "City" };
        string[] phase2_FieldValues = new string[] { "Peter", "+122-2222222", "London" };
        // Perform the mail merge.
        document.MailMerge.Execute(phase2_FieldNames, phase2_FieldValues);
 
        // Third phase merge field execution.
        string[] phase3_FieldName = new string[] { "Country" };
        string[] phase3_FieldValue = new string[] { "United Kingdom" };
        // Perform the mail merge.
        document.MailMerge.Execute(phase3_FieldName, phase3_FieldValue);
        // Create file stream.
        using (FileStream outputFileStream = new FileStream(Path.GetFullPath("../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
        {
            // Save the Word document to file stream.
            document.Save(outputFileStream, FormatType.Docx); 
        }
    }
}

A complete working sample to perform a mail merge for the required fields in a Word document using C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows:

Output Word document generated in ASP.NET Core

Take a moment to peruse the documentation, where you can find basic Word document processing options along with the 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.

See Also:

How to perform a mail merge using Dictionary as a data source

Mail merge from Excel to Word in C#, VB.NET

How to mail merge a Word document in ASP.NET

Note:

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.

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