We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Newbie mail merge query

Hi,

I have got my mail merge query working OK - But what I would like to do is if a field has a certain value in I want to insert a few lines of text instead of the value on tha data table.

Can any one let me know if this can be done and how I would do it?

Thanks

Frank

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

Thanks for your inquiry. I think you can use MergeField event handler in this case. See the following link for more information:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/the-mergefield-event.html

Also see the following code:

public void Test187()

{

string[] names = { "field1", "field2", "field3" };

object[] values = { "value1", "value1", "replace" };

//Open template

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

//Add MergeField event handler

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

//Execute mail merge

doc.MailMerge.Execute(names, values);

//Save output document

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

}

void MailMerge_MergeField_ReplaceValue(object sender, MergeFieldEventArgs e)

{

if (e.FieldValue.Equals("replace"))

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

//Move document builder cursor to mergefield

builder.MoveToMergeField(e.FieldName);

//Insert some text

builder.Write("Hello from MergeField event handler!");

}

}

Hope this helps.

Best regards.