MailMergeExecute Method (DataTable)

Performs mail merge from a DataTable into the document.

Namespace:  Aspose.Words.MailMerging
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public void Execute(
	DataTable table
)

Parameters

table
Type: System.DataDataTable
Table that contains data to be inserted into mail merge fields. Field names are not case sensitive. If a field name that is not found in the document is encountered, it is ignored.
Remarks

Use this method to fill mail merge fields in the document with values from a DataTable.

All records from the table are merged into the document.

You can use NEXT field in the Word document to cause MailMerge object to select next record from the DataTable and continue merging. This can be used when creating documents such as mailing labels.

When MailMerge object reaches end of the main document and there are still more rows in the DataTable, it copies entire content of the main document and appends it to the end of the destination document using a section break as a separator.

This method ignores the RemoveUnusedRegions option.

Examples
Executes mail merge from an ADO.NET DataTable.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD CustomerName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD Address ");

// 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(ArtifactsDir + "MailMerge.ExecuteDataTable.doc");

// Create a copy of our document to perform another mail merge
doc = new Document();
builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD CustomerName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD Address ");

// We can also source values for a mail merge from a single row in the table
doc.MailMerge.Execute(table.Rows[1]);

doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.OneRow.doc");
See Also