Execute Mail Merge operation 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.

Merge Fields Screenshot

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

Upload your file(Drag files here)

Download an input 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
    static void Main(string[] args)
        // If using Professional version, put your serial key below.

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

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


        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.

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

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


        document.Save("Merge Fields.docx")

    End Sub

End Module