Merge Ranges

Mail merge can be performed on a whole document or a part of the document, which is then called a named range mail merge.

Following sample shows how to perform a mail merge on a named range and shows how to chain mail merge operations (perform multiple successive mail merge operations on the same document).

Screenshot

Merge Ranges Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.IO;
5using System.Linq;
6using GemBox.Document;
7using GemBox.Document.MailMerging;
8
9class Sample
10{
11    [STAThread]
12    static void Main(string[] args)
13    {
14        // If using Professional version, put your serial key below.
15        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
16
17        DocumentModel document = DocumentModel.Load("InvoiceForMailMerge.docx");
18
19        int numberOfItems = 10;
20
21        // Fill items (amount and prices).
22        DataTable dt = new DataTable()
23        {
24            TableName = "Item"
25        };
26        dt.Columns.Add("Date", typeof(DateTime));
27        dt.Columns.Add("Hours", typeof(double));
28        dt.Columns.Add("Price", typeof(double));
29        dt.Columns.Add("Total", typeof(double));
30
31        DateTime startDate = DateTime.Now.AddDays(-numberOfItems);
32        for (int i = 0; i < numberOfItems; i++)
33        {
34            // We worked between 6 and 8 hours per day.
35            int workHours = i % 3 + 6;
36            double totalPrice = workHours * 35;
37
38            dt.Rows.Add(startDate.AddDays(i), workHours, 35, totalPrice);
39        }
40
41        document.MailMerge.Execute(dt);
42
43        // Fill invoice number and date.
44        int invoiceNumber = 14;
45        document.MailMerge.Execute(
46            new
47            {
48                Number = invoiceNumber,
49                InvoiceDate = DateTime.Now.ToShortDateString()
50            });
51
52
53        // Fill customer data.
54        document.MailMerge.Execute(
55            new
56            {
57                Name = "ACME Corp",
58                Address = "240 Old Country Road, Springfield, IL",
59                Country = "USA",
60                ContactPerson = "Joe Smith"
61            });
62
63        // Fill total.
64        document.MailMerge.Execute(new { TotalPrice = dt.Rows.Cast<DataRow>().Sum(row => (double)row["Total"]) });
65
66        // Fill notes.
67        document.MailMerge.Execute(new { Notes = "Payment via check." });
68
69        document.Save("Merge Ranges.docx");
70    }
71}
1Imports System
2Imports System.Collections.Generic
3Imports System.Data
4Imports System.IO
5Imports System.Linq
6Imports GemBox.Document
7Imports GemBox.Document.MailMerging
8
9Module Samples
10
11    Sub Main()
12
13        ' If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
15
16        Dim document As DocumentModel = DocumentModel.Load("InvoiceForMailMerge.docx")
17
18        Dim numberOfItems As Integer = 10
19
20        ' Fill items (amount and prices).
21        Dim dt As New DataTable()
22        dt.TableName = "Item"
23        dt.Columns.Add("Date", GetType(DateTime))
24        dt.Columns.Add("Hours", GetType(Double))
25        dt.Columns.Add("Price", GetType(Double))
26        dt.Columns.Add("Total", GetType(Double))
27
28        Dim startDate As DateTime = DateTime.Now.AddDays(-numberOfItems)
29        For i As Integer = 0 To numberOfItems - 1
30            ' We worked between 6 and 8 hours per day.
31            Dim workHours As Integer = i Mod 3 + 6
32            Dim totalPrice As Double = workHours * 35
33
34            dt.Rows.Add(startDate.AddDays(i), workHours, 35, totalPrice)
35        Next
36
37        document.MailMerge.Execute(dt)
38
39        ' Fill invoice number and date.
40        Dim invoiceNumber As Integer = 14
41        document.MailMerge.Execute(New With {
42            .Number = invoiceNumber,
43            .InvoiceDate = DateTime.Now.ToShortDateString()
44        })
45
46        ' Fill customer data.
47        document.MailMerge.Execute(New With {
48            .Name = "ACME Corp",
49            .Address = "240 Old Country Road, Springfield, IL",
50            .Country = "USA",
51            .ContactPerson = "Joe Smith"
52        })
53
54        ' Fill total.
55        document.MailMerge.Execute(New With {
56            .TotalPrice = dt.Rows.Cast(Of DataRow)().Sum(Function(row) CDbl(row("Total")))
57        })
58
59        ' Fill notes.
60        document.MailMerge.Execute(New With {
61            .Notes = "Payment via check."
62        })
63
64        document.Save("Merge Ranges.docx")
65
66    End Sub
67
68End Module

Check next sample or find out more about GemBox.Document and GemBox Software.