Mail Merge in Word with C# and VB.NET

Mail merge is the process of merging or importing data from a data source to a Word document. In essence, the process involves replacing the Field elements of the MergeField type with values from C# or VB.NET objects.

GemBox.Document exposes all mail merge related operations and options through the DocumentModel.MailMerge property.

You can read the full documentation on the Mail Merge help page.

The following example shows how you can perform a simple mail merge by importing data from an anonymous type object.

Creating Word document with mail merge process in C# and VB.NET
Screenshot of Word file created from mail merge
Upload your file (Drag file here)
using System;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%InputFileName%");

        // Create data source for mail merge process.
        var data = new
        {
            Number = 10203,
            Date = DateTime.Now,
            Company = "ACME Corp",
            Address = "240 Old Country Road, Springfield, IL",
            Country = "USA",
            FullName = "Joe Smith"
        };

        // Execute mail merge process.
        document.MailMerge.Execute(data);

        document.Save("Mail Merge Output.%OutputFileType%");
    }
}
Imports System
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%InputFileName%")

        ' Create data source for mail merge process.
        Dim data = New With
        {
            .Number = 10203,
            .Date = DateTime.Now,
            .Company = "ACME Corp",
            .Address = "240 Old Country Road, Springfield, IL",
            .Country = "USA",
            .FullName = "Joe Smith"
        }

        ' Execute mail merge process.
        document.MailMerge.Execute(data)

        document.Save("Mail Merge Output.%OutputFileType%")

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.