Free Support Forum - aspose.com

MergeField event and Regions

Hello,

Is MergeField event fired for a Region if corresponding DataTable is empty? I was stepping through in debugger and it appears that MergeField event fired only for DataTables that have at least 1 row.
What I try to accomplish is to replace all merge fields that have no value (have value of null, DBNull, or corresoponding DataTable has no rows) with specific text. I tried to do it through MergeField event hoping it will be fired for all fields and all DataTable but it does not seem to be the case. Is there any other way to do it?

Thanks

Hi

Thanks for your inquiry. You is absolutely right, MergeField event is fired only for DataTables that have at least 1 row. I think that you can try using the following code to solve your problem.

public void TestMailMerge_105168()

{

DataTable table = new DataTable("table");

table.Columns.Add("Field1");

table.Columns.Add("Field2");

if (table.Rows.Count == 0)

{

DataRow row = table.NewRow();

table.Rows.Add(row);

}

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

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

doc.MailMerge.ExecuteWithRegions(table);

doc.Save(@"399_105168_lubimkas\out.doc");

}

void MailMerge_MergeField_105168(object sender, MergeFieldEventArgs e)

{

if (e.FieldValue == DBNull.Value || e.FieldValue == null)

{

e.Text = "Null value";

}

}

I hope that this will help you. Please let me know if you would like to know something else.

Best regards.