Articles in this section
Category / Section

How to perform mail merge for required fields in 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 field in a Word document using C#

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

Steps to perform mail merge for required field in 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 packages 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 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 mail merge for the required field 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, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion® Word Framework features.

See Also:

How to perform mail merge using Dictionary as data source

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

How to mail merge Word document in ASP.NET

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
Please  to leave a comment
Access denied
Access denied