Free Support Forum - aspose.com

MergeField unexpected behaviour

Please find code attached and a basic word template.

Note if you move

srcDoc.MailMerge.ExecuteWithRegions(Table1);

below

srcDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(MailMerge_MergeField);

It works as you would expect, however why would ExecuteWithRegions replace null fields outside 'TableStart' & 'TableEnd' tags. Unless you use the SQL server ISNULL function you would never be able to programatically determine the output of a report containing both ExecuteWithRegions & MergeField

Let me know if you require further information, the text "Error" should not be shown on the report.

Regards

Chris

Hi

Thanks fro your inquiry. I think that you should first call ExecuteWithRegions and then call Execute method. See the following code example.

public void Test024()

{

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

srcDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(MailMerge_MergeField_024);

srcDoc.MailMerge.ExecuteWithRegions(Table1);

foreach (DataRow row in Table2.Rows)

{

srcDoc.MailMerge.Execute(row);

}

srcDoc.Save(@"Test024\out.doc");

}

private void MailMerge_MergeField_024(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)

{

if (e.FieldValue == null)

{

e.Text = "Error";

}

}

I hope this could help you.

Best regards.

Thank you, i was already aware of the workaround when i posted the original message. The point i am trying to make is you can not set a fieldvalue when the row is null.

Here is an example with a subtle change, note i have switched the code as you have asked.

Thanks

private void Form1_Load(object sender, EventArgs e)

{

Aspose.Words.Document srcDoc = new Aspose.Words.Document("template.doc");

srcDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(MailMerge_MergeField);

srcDoc.MailMerge.ExecuteWithRegions(Table1);

foreach (DataRow row in Table2.Rows)

{

srcDoc.MailMerge.Execute(row);

}

srcDoc.Save(@"out.doc");

}

private void MailMerge_MergeField(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)

{

if (e.FieldValue == null)

{

e.Text = "Error";

}

}

private DataTable Table1

{

get

{

DataTable dt = new DataTable();

dt.TableName = "Table1";

dt.Columns.Add("Field1");

dt.Columns.Add("Field2");

DataRow dr = dt.NewRow();

dr[0] = "one";

dr[1] = "1";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "two";

dr[1] = "2";

dt.Rows.Add(dr);

return dt;

}

}

private DataTable Table2

{

get

{

DataTable dt = new DataTable();

dt.TableName = "Table2";

dt.Columns.Add("FullName");

DataRow dr = dt.NewRow();

dr[0] = null; // previously: "John Smith";

dt.Rows.Add(dr);

return dt;

}

}

Hi

Thanks for additional information. Maybe you can try using the following code snippet.

private void MailMerge_MergeField(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)

{

if (string.IsNullOrEmpty(e.FieldValue.ToString()))

{

e.Text = "Error";

}

}

Hope this helps.

Best regards.