DocumentMailMerge Property
Returns a MailMerge object that represents the mail merge functionality for the document.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 17.10
Syntax
public MailMerge MailMerge { get; }

Property Value

Type: MailMerge
Examples
Executes mail merge from an ADO.NET DataTable.
[C#]

Document doc = new Document(MyDir + "MailMerge.ExecuteDataTable.doc");

// This example creates a table, but you would normally load table from a database. 
DataTable table = new DataTable("Test");
table.Columns.Add("CustomerName");
table.Columns.Add("Address");
table.Rows.Add(new object[] {"Thomas Hardy", "120 Hanover Sq., London"});
table.Rows.Add(new object[] {"Paolo Accorti", "Via Monte Bianco 34, Torino"});

// Field values from the table are inserted into the mail merge fields found in the document.
doc.MailMerge.Execute(table);

doc.Save(MyDir + "MailMerge.ExecuteDataTable Out.doc");
[Visual Basic]

Dim doc As New Document(MyDir & "MailMerge.ExecuteDataTable.doc")

' This example creates a table, but you would normally load table from a database. 
Dim table As New DataTable("Test")
table.Columns.Add("CustomerName")
table.Columns.Add("Address")
table.Rows.Add(New Object() {"Thomas Hardy", "120 Hanover Sq., London"})
table.Rows.Add(New Object() {"Paolo Accorti", "Via Monte Bianco 34, Torino"})

' Field values from the table are inserted into the mail merge fields found in the document.
doc.MailMerge.Execute(table)

doc.Save(MyDir & "MailMerge.ExecuteDataTable Out.doc")
Examples
Executes a mail merge with repeatable regions.
[C#]

public void ExecuteWithRegionsDataTable()
{
    Document doc = new Document(MyDir + "MailMerge.ExecuteWithRegions.doc");

    int orderId = 10444;

    // Perform several mail merge operations populating only part of the document each time.

    // Use DataTable as a data source.
    DataTable orderTable = GetTestOrder(orderId);
    doc.MailMerge.ExecuteWithRegions(orderTable);

    // Instead of using DataTable you can create a DataView for custom sort or filter and then mail merge.
    DataView orderDetailsView = new DataView(GetTestOrderDetails(orderId));
    orderDetailsView.Sort = "ExtendedPrice DESC";
    doc.MailMerge.ExecuteWithRegions(orderDetailsView);

    doc.Save(MyDir + "MailMerge.ExecuteWithRegionsDataTable Out.doc");
}

private static DataTable GetTestOrder(int orderId)
{
    DataTable table = ExecuteDataTable(string.Format(
        "SELECT * FROM AsposeWordOrders WHERE OrderId = {0}", orderId));
    table.TableName = "Orders";
    return table;
}

private static DataTable GetTestOrderDetails(int orderId)
{
    DataTable table = ExecuteDataTable(string.Format(
        "SELECT * FROM AsposeWordOrderDetails WHERE OrderId = {0} ORDER BY ProductID", orderId));
    table.TableName = "OrderDetails";
    return table;
}

/// <summary>
/// Utility function that creates a connection, command, 
/// executes the command and return the result in a DataTable.
/// </summary>
private static DataTable ExecuteDataTable(string commandText)
{
    // Open the database connection.
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
        DatabaseDir + "Northwind.mdb";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Open();

    // Create and execute a command.
    OleDbCommand cmd = new OleDbCommand(commandText, conn);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataTable table = new DataTable();
    da.Fill(table);

    // Close the database.
    conn.Close();

    return table;
}
[Visual Basic]

Public Sub ExecuteWithRegionsDataTable()
    Dim doc As New Document(MyDir & "MailMerge.ExecuteWithRegions.doc")

    Dim orderId As Integer = 10444

    ' Perform several mail merge operations populating only part of the document each time.

    ' Use DataTable as a data source.
    Dim orderTable As DataTable = GetTestOrder(orderId)
    doc.MailMerge.ExecuteWithRegions(orderTable)

    ' Instead of using DataTable you can create a DataView for custom sort or filter and then mail merge.
    Dim orderDetailsView As New DataView(GetTestOrderDetails(orderId))
    orderDetailsView.Sort = "ExtendedPrice DESC"
    doc.MailMerge.ExecuteWithRegions(orderDetailsView)

    doc.Save(MyDir & "MailMerge.ExecuteWithRegionsDataTable Out.doc")
End Sub

Private Shared Function GetTestOrder(ByVal orderId As Integer) As DataTable
    Dim table As DataTable = ExecuteDataTable(String.Format("SELECT * FROM AsposeWordOrders WHERE OrderId = {0}", orderId))
    table.TableName = "Orders"
    Return table
End Function

Private Shared Function GetTestOrderDetails(ByVal orderId As Integer) As DataTable
    Dim table As DataTable = ExecuteDataTable(String.Format("SELECT * FROM AsposeWordOrderDetails WHERE OrderId = {0} ORDER BY ProductID", orderId))
    table.TableName = "OrderDetails"
    Return table
End Function

''' <summary>
''' Utility function that creates a connection, command, 
''' executes the command and return the result in a DataTable.
''' </summary>
Private Shared Function ExecuteDataTable(ByVal commandText As String) As DataTable
    ' Open the database connection.
    Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseDir & "Northwind.mdb"
    Dim conn As New OleDbConnection(connString)
    conn.Open()

    ' Create and execute a command.
    Dim cmd As New OleDbCommand(commandText, conn)
    Dim da As New OleDbDataAdapter(cmd)
    Dim table As New DataTable()
    da.Fill(table)

    ' Close the database.
    conn.Close()

    Return table
End Function
See Also