Free Support Forum -

Mail Merge Fields


We are currently evaluating Aspose.Words for our application. I was wondering if you could suggest the easiest way for us to migrate given the following scenario.

Currently our application generates documents in HTML. We have HTML templates with our own mail merge fields which use the square brackets ([]) to identify. e.g. [MailMergeField1]. Then we build a new document by copying the HTML template and replacing all the mail merge fields giving a final HTML document. Our mail merge fields may result in the addition of a single word through to an entire block of formatted HTML.

Ideally we would like to be able to continue using these HTML templates in the short term. So it would be a case of finding the mail merge field and replacing it with HTML (I couldn't quite work out how to do that using the API). The options to find didn't seem to include a text string and the insert HTML seemed to imply that it would be inserted prior to the paragraph start - whereas we need to add text within the paragraph.

Any comments would be appreciated.



Thank you for your interest in Aspose products. I think that you can use ReplaceEvaluator to achieve this. For example see the following code. This code replaces [text] with some HTML.

public void TestParseHtml_101149()


Document doc = new Document(@"328_101149_DaleBurrell\in.html");

Regex regex = new Regex("\\[text\\]");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction_101149), false);



ReplaceAction ReplaceAction_101149(object sender, ReplaceEvaluatorArgs e)


Run run = (Run)e.MatchNode;

Paragraph para = run.ParentParagraph;

DocumentBuilder builder = new DocumentBuilder(para.Document);

// We are using \xbf (inverted question mark) symbol for temporary purposes.

// Any symbol will do that is non-special and is guaranteed not to be presented in the document.

// The purpose is to split matched run in two and insert HTML between them

para.Range.Replace(e.Match.Value, "\xbf", true, false);

Run subrun = (Run)run.Clone(false);

int pos = run.Text.IndexOf("\xbf");

subrun.Text = subrun.Text.Substring(0, pos);

run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1);

para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subrun);






// Let's remove run if it is empty.

if (run.Text == "")


// No replace action is necessary - we have already done what we intended to do.

return ReplaceAction.Skip;


I hope that this will help you.

Also see the following links.

Best regards.