We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to mail merge into more that one specific merge field

Good Day


I want to apply mail merge to a document which have 4 name, surname and birthday fields.
The reason why I did this is to save paper.
How can I use a loop to print each merge field with a different row in the data source.

I have uploaded an example to make it more clear

Thanks in advance

Hi Johnane,


Thanks for your inquiry. I have attached a modified version of your template document and please use the following code to meet this requirement:

Document doc = new Document(MyDir + @“Merge
example-modified.docx”
);

doc.MailMerge.MergeDuplicateRegions = true;

doc.MailMerge.UseWholeParagraphAsRegion = false;

doc.MailMerge.ExecuteWithRegions(GetDataTable());

doc.Save(MyDir + @"16.1.0.docx");


private static DataTable GetDataTable()

{

DataTable dataTable = new DataTable("tbl");

dataTable.Columns.Add(new DataColumn("Name"));

dataTable.Columns.Add(new DataColumn("Surname"));

dataTable.Columns.Add(new DataColumn("Birthday"));

DataRow dataRow;

for (int i = 0; i < 4; i++)

{

dataRow = dataTable.NewRow();

dataRow[0] = "Johnane" + i;

dataRow[1] = "Umslana" + i;

dataRow[2] = DateTime.Now.ToString("dd/MMM/yyyy") + i;

dataTable.Rows.Add(dataRow);

}

return dataTable;

}


Hope, this helps.

Best regards,