MergeImage

Hi folks,

when using MergeImage I find that the Image is only merged in/or the event is only called when I call doc.MailMerge.Execute(...) and not with doc.MailMerge.ExecuteWithRegions(...). Has anyone else encountered this behaviour?

/Mark

Hi Mark,

Thank you for considering Aspose.

Please make sure the template is properly formed, see say the Employees Report demo as an example. Also, check that the data source field names properly map to the merge field names. If nothing helps, please attach the template and post a fragment of code you use for performing mail merge.

Please find enclosed the template that is to be populated. I'm trying to find a user from the dataset and then swap in the appropriate gif using the MergeImageEvent

The XML file would look like:

<docs>
<indexsection>
<state>Illinois</state>
<areacode>U12</areacode>
<docid>0000036879</docid>
<doctypeid>13</doctypeid>
<underwritercode>BE001</underwritercode>
<effectivedate>20030302</effectivedate>
<expirydate>20040302</expirydate>
<prebindterrorism>
</prebindterrorism>
</indexsection>
<docs>

If I run it using Execute (as shown below) it works perfectly fine:

internal override Document Execute()
{
//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "ImageTesting.doc"));

DataSet dataSet = new DataSet();
dataSet.ReadXml(System.IO.Path.Combine(DocPath, "MarkDemo.xml"));

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
foreach( DataTable currentTable in dataSet.Tables)
{
if (currentTable.TableName.Equals("indexsection"))
{
for (int i = 0; i < currentTable.Rows.Count; i++)
{
DataRow indexsectionRow = currentTable.RowsIdea [I];
doc.MailMerge.Execute(indexsectionRow);
}
}
else
{
doc.MailMerge.ExecuteWithRegions(currentTable);
}
}

MemoryStream streamOut = new MemoryStream();
doc.Save(streamOut, SaveFormat.FormatDocument);
return doc;
}

private void HandleMergeImage(object sender, MergeImageFieldEventArgs e)
{
if (e.FieldName == "underwritercode")
{
string shortFileName = e.FieldValue as string;
e.ImageFileName = System.IO.Path.Combine(DocPath, shortFileName + ".gif");
}
}

If I run it using ExecuteWithRegions (as shown below) it doesn't work:

internal override Document Execute()
{
//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "ImageTesting.doc"));

DataSet dataSet = new DataSet();
dataSet.ReadXml(System.IO.Path.Combine(DocPath, "MarkDemo.xml"));

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
foreach( DataTable currentTable in dataSet.Tables)
{
doc.MailMerge.ExecuteWithRegions(currentTable);
}

MemoryStream streamOut = new MemoryStream();
doc.Save(streamOut, SaveFormat.FormatDocument);
return doc;
}

private void HandleMergeImage(object sender, MergeImageFieldEventArgs e)
{
if (e.FieldName == "underwritercode")
{
string shortFileName = e.FieldValue as string;
e.ImageFileName = System.IO.Path.Combine(DocPath, shortFileName + ".gif");
}
}

I reproduced your issue, figured out that Aspose.Word failed to recognize merge regions in your test template and only then noticed that it was improperly formed… Smile [:)] You should use TableStart and TableEnd instead of StartTable and EndTable.

Embarrassed [:$]You’re a star dude. Sorry for not spotting that myself