Free Support Forum - aspose.com

MailMerge with regions unexpected behavior

Hello. I try to use MailMerge with regions to generate document with text like this:

Re:Payment of our Debit note No 1053, 1054, 1055, 1096.

To get it I use mail merge fields in following form:

Re: Payment of our Debit note No «TableStart:Bills» «BillNumber», «TableEnd:Bills»

The expected result of construction described abobe is:

Re:Payment of our Debit note No 1053, 1054, 1055, 1096,

But actual result is:

Re:Payment of our Debit note No 1053,

Re:Payment of our Debit note No 1054,

Re:Payment of our Debit note No 1055,

Re:Payment of our Debit note No 1096,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />So, if I understand correct, during performing mail merge, the whole paragraph is duplicated, but not "content that is included inside a mail merge region will be automatically repeated for every record in the data source" as it described in Aspose.Words documentation.

Please advice, how I can get the expected result of mail merge using regions.

Thank you.

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

Thanks for your inquiry. I don’t think that you need mail merge with regions in this case. I think that you should use MergeField event handler. Please see the following code:

public void Test003()

{

//For example you have datatable that contains BillNumbers

DataTable billNumbersTable = new DataTable();

billNumbersTable.Columns.Add("BillNumber", typeof(int));

//Add few billNumbers to table

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

{

billNumbersTable.Rows.Add(new object[] { i });

}

//Create string array that contains field names

//In our case it contains only one filed name "BillNumber"

string[] names = { "BillNumber" };

//As a value of this field we will use DataTable

object[] values = { billNumbersTable };

//Open template document

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

//Add MergeField event handler

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

//Execute mail merge

doc.MailMerge.Execute(names, values);

//Save result

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

}

void MailMerge_MergeField003(object sender, MergeFieldEventArgs e)

{

//Check name foo field

if (e.FieldName == "BillNumber")

{

string result = string.Empty;

//Get datatable from field value

DataTable dt = e.FieldValue as DataTable;

if (dt != null)

{

foreach (DataRow row in dt.Rows)

{

if (!row.Equals(dt.Rows[0]))

result += ", ";

result += row["BillNumber"].ToString();

}

}

//Insert text into the mergefield

e.Text = result;

}

}

Also see attached documents.

Hope this helps.

Best regards.

Alexey, thank for the answer.

I think you'll agree that solution that you provided need significantly more development effort than using regions variant. Also, unfortunally it does not work out for our product, where we develop template designer for end users, and then they can design any report types that they want.

Could you suggest other solution for the issue?

Thank you,

As another solution you can use NEXT fields. But in this case you should know how match records will be inserted.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Also you can try automatically inserting additional mergefields during mail merge. See the following code:

public void Test003()

{

//Open template document

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

//Add MergeField event handler

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

//Execute mail merge

foreach (DataRow row in billNumbersTable.Rows)

{

doc.MailMerge.Execute(row);

}

//Remove non mered fields

doc.MailMerge.DeleteFields();

//Save result

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

}

void MailMerge_MergeField003(object sender, MergeFieldEventArgs e)

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

builder.Write(", ");

builder.InsertField(string.Format("MERGEFIELD {0}", e.FieldName), "");

}

Best regards.

So, Alexey, could you just confirm that mail merge with regions repeat whole paragraph (in the situation described above) by design and it’s not a bug?

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

Yes this is by design. It is not a bug.

Best regards.