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

Free Support Forum - aspose.com

Newbie mail merge query


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?



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:


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



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


//Insert some text

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



Hope this helps.

Best regards.