Free Support Forum - aspose.com

Repeat 2 table rows using MailMerge.ExecuteWithRegions

Hi,

My word template contains this table:

Frequent Flyer Information<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Recorded Value

New Value

(leave blank if recorded value is correct)

«TableStart:FF»Airline / Program

«FrequentFlyerDesc»

Membership Number

«FrequentFlyerNumber»

«TableEnd:FF»

But when running ExecuteWithRegions I get this error:

Mail merge region 'FF' is badly formed. TableStart and TableEnd should be in the same section, same table row or same table cell.

Basically I want to repeat these two table rows for each dataitem. How can I do it?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that you can achieve this using the following template. (Also see the attached documents)

Recorded Value

New Value

(leave blank if recorded value is correct)

«TableStart:FF»

Airline / Program

«FrequentFlyerDesc»

Membership Number

«FrequentFlyerNumber»

«TableEnd:FF»

Here is code example for you.

public void Test110()

{

//Prepare data

DataTable table = new DataTable("FF");

table.Columns.Add("FrequentFlyerDesc");

table.Columns.Add("FrequentFlyerNumber");

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

{

DataRow row = table.NewRow();

row["FrequentFlyerDesc"] = "test_" + i.ToString();

row["FrequentFlyerNumber"] = "test_" + i.ToString();

table.Rows.Add(row);

}

//open template

Document doc = new Document(@"Test110\in.doc");

//Add MergeField event handler

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField110);

//Execute mail merge

doc.MailMerge.ExecuteWithRegions(table);

//Save output document

doc.Save(@"Test110\out.doc");

}

void MailMerge_MergeField110(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "FrequentFlyerNumber")

{

//Get parent Table

Table myTable = (e.Field.Start.ParentParagraph.ParentNode.ParentNode.ParentNode as Table);

//Remove empty peragraph after table

myTable.NextSibling.Remove();

//Remove empty paragraphbefor table

myTable.PreviousSibling.Remove();

}

}

I hope this could help you.

Best regards.