MailMerge ExecuteWithRegions with multiple datatables

Hi There,
My word doc has the below structure where each table needs to have 3 rows. So if there are 9 rows, they would get split into 3 tables with 3 rows each with their header columns.

Column1 Column2 Column3 Column4
<TableStart:Items>Data Data Data Data<TableEnd:Items>

Test Code:

foreach (DataTable dataTable in dataTablesList)
{
   dataTable.TableName = "Items";
   doc.MailMerge.ExecuteWithRegions(dataTable);
}

The output doesn’t seem to separate the header columns and repeat the items with the header column names correctly.

Expected Output:

Column1 Column2 Column3 Column4
Data Data Data Data
Data Data Data Data
Data Data Data Data

Column1 Column2 Column3 Column4
Data Data Data Data
Data Data Data Data
Data Data Data Data

Is there a way to do this using MailMerge? Any help would be appreciated.

Hi Chitrang,

Thanks for your inquiry. In your case, we suggest you please create a table with four rows (first row is header) and insert the mail merge fields with NEXT field as shown in attached image.

Please use following code example with attached template document. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DataTable dt = new DataTable("Items");
dt.Columns.Add("item1", typeof(String));
dt.Columns.Add("item2", typeof(String));
DataRow dr = dt.NewRow();
dr["item1"] = "item1 1";
dr["item2"] = "item2 1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["item1"] = "item1 2";
dr["item2"] = "item2 2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["item1"] = "item1 3";
dr["item2"] = "item2 3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["item1"] = "item1 4";
dr["item2"] = "item2 4";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["item1"] = "item1 5";
dr["item2"] = "item2 5";
dt.Rows.Add(dr);
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(MyDir + "Out.docx");

Thanks a lot Tahir…worked like a charm! :slight_smile: