Free Support Forum - aspose.com

MailMerge MergeField Event

Hello,

When I try and perform a mailmerge on a particular document, using a dataset, the finished document does not contain the field values, they are blank. I am using the MergeField event. I never used to have the code in place to cope with when e.FieldValue = null - I added it to stop the mailmerge falling over, but the FieldValue shouldn't be null (see attached dataset). When I do not use the MergeField event it appears to work ok. Any assistance would be very much appreciated.

Thank you in advance.

My code is as follows:

Document doc = new Document(@"F:\temp\RPF25_Fragment.xml");

doc.MailMerge.MergeField +=

new Aspose.Words.Reporting.MergeFieldEventHandler(ProcessField);

DataSet ds = new DataSet("CJ");

ds.ReadXml(

@"F:\temp\DataSet.xml");

string[] fieldNames = doc.MailMerge.GetFieldNames();

foreach (DataTable dt in ds.Tables)

{

bool bRegion = false;

bool bProcess = false;

for (int i = 0; i < fieldNames.Length; i++)

{

try

{

if (dt.Columns.Contains(fieldNames[i]))

{

bProcess = true;

}

if (fieldNames[i].Substring(0, 11) == "TableStart:")

{

string actFN = fieldNames[i + 1];

if (dt.Columns.Contains(actFN))

{

bRegion = true;

bProcess = true;

break;

}

}

}

catch (System.ArgumentOutOfRangeException ex)

{

// Do nothing

}

}

if (bProcess == true)

{

if (bRegion == true)

{

doc.MailMerge.ExecuteWithRegions(dt);

}

else

{

doc.MailMerge.Execute(dt);

}

}

}

// Delete any leftover fields

doc.MailMerge.DeleteFields();

doc.Save(@"F:\temp\1d82ca00-b7fc-4cea-aca1-16f935219ae5.doc");

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

{

// Cope with NULL value in dataset

if ((e.FieldValue == null) || (e.FieldValue.ToString().ToUpper() == "NULL"))

{

e.Text = "";

return;

}

// BarCode - force font

NameValueCollection bc =

(NameValueCollection) ConfigurationManager.GetSection("MergeFieldSettings/BarCode");

// ShortDate - manipulate data

NameValueCollection sd =

(NameValueCollection)ConfigurationManager.GetSection("MergeFieldSettings/ShortDate");

if (bc[e.FieldName] != null)

{

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

builder.Font.Name = "IDAutomationHC39M";

builder.Write((string)e.FieldValue);

return;

}

if (e.FieldName.EndsWith("_SHORT"))

{

string sDte = e.FieldName.Substring(0, e.FieldName.Length - 6);

if (sd[sDte] != null)

{

try

{

DateTime dt = DateTime.Parse((string) e.FieldValue);

e.Text = dt.ToShortDateString(); // ToString("dd MMMM yyyy");

}

catch (Exception ex)

{

e.Text = "";

}

}

return;

}

// Everything else set text to be fieldvalue

e.Text = e.FieldValue.ToString();

}

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

Thanks for your request. This occurs because your DataSet contains two tables. One field is stored in firs table and two fields are stored in second one. When you execute mail merge you have the following code in the beginning of MergeField event handler:

if ((e.FieldValue == null) || (e.FieldValue.ToString().ToUpper() == "NULL"))

{

e.Text = "";

return;

}

And this code does not allow processing the second table (When you start processing of the second table the template document already does not contain any merge fields.). There is very simple solution – Try using the following code:

if ((e.FieldValue == null) || (e.FieldValue.ToString().ToUpper() == "NULL"))

{

return;

}

Best regards.

Thank you very much - it was a simple fix!

Floyd