GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Mail Merge in Word with C# and VB.NET

Mail merge is a process of merging or importing data from a data source to a document.

Following example demonstrates how to perform a simple mail merge by importing data from an instance of an anonymous type.

Input template document also defines formatting for DateTime field using the field date/time formatting switch (\@).

To view field switches in Microsoft Word, press Alt + F9 to toggle field codes.

Field numeric switch (\#) is also supported for formatting numeric data types in the mail merge process.

Screenshot
Merge Fields Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download a sample file

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using GemBox.Document;
using GemBox.Document.MailMerging;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = DocumentModel.Load("MergeFields.docx");

        var customer = new { CustomerName = "John", Surname = "Doe", Date = DateTime.Now };

        document.MailMerge.Execute(customer);

        document.Save("Merge Fields.docx");
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.MailMerging

Module Samples

    Sub Main()

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

        Dim document As DocumentModel = DocumentModel.Load("MergeFields.docx")

        Dim customer = New With {.CustomerName = "John", .Surname = "Doe", .Date = DateTime.Now}

        document.MailMerge.Execute(customer)

        document.Save("Merge Fields.docx")

    End Sub

End Module

Check next sample.