Free Support Forum -

Start with Merge Fields that are inside bookmark

Ok this is a little complicated. I have a word document that contains 1 to 500 possible merge fields and 1 to 600 bookmarks. Each mergefield is inside a bookmark named after the mergefield plus a number. That leaves 100 bookmarks that contain text or tables. For this current issue I am only dealing with the mergefields and the their surrounding bookmarks.

I load the word document into Aspose.Words document and perform mail merge. Open the file and everything is fine. I change some of the data in the dataset that is applied during the mail merge. Then I load the word document into Aspose.Words document and perform the following steps using code.

I search for each bookmark that matches the dataset's variables and remove the data between the bookmarks. Then I loop back through again and insert merge fields into the bookmarks with matching names minus the numbers append for uniqueness. Finally I run the merge again. The data updates but the <> shows up as text next to the updated field text in the resulting word file.

I have attached the original word file and the result.



Hi Bryan,

Thanks for your request. There is something strange in your original template. There are some fields without field codes. These fields causes the problem. I created a simple programmatic workaround for you (see the highlighted code):

// Create a simple datasource for mail merge.

string[] names = new string[] { "TitleofDocument", "CompanyName", "ValuationPurpose", "ValuationDate",

"ControllingMinority", "FairMarketValue", "SharesOutstanding1", "StockTableXRound",

"SharesOutstanding2", "ReportMonthYear", "StockTableYRound" };

// We will use this list to check whether bookmark corresponds a mergefild.

ArrayList mergeFieldNames = new ArrayList(names);

// ----------==============STAGE 1==============----------

// Open tempalte.

Document doc = new Document(@"Test001\Stock2Class2.docx");

// Execute mail merge.

doc.MailMerge.Execute(names, names);

// There are some strange fields in your document without field code. We shoudl remove them.

Node[] fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true).ToArray();

foreach (Node fieldStart in fieldStarts)


// If field separator is followed by fieldstart we can suppose the field does nto have field code,

// This means the field is corrupted and we have to remove it.

if (fieldStart.NextSibling != null && fieldStart.NextSibling.NodeType == NodeType.FieldSeparator)


Node currentNode = fieldStart;

while (currentNode!=null &&currentNode.NodeType!=NodeType.FieldEnd)


Node nextNode = currentNode.NextSibling;

// remove current node. We should not remove bookmark nodes.

if (currentNode.NodeType != NodeType.BookmarkStart && currentNode.NodeType != NodeType.BookmarkEnd)


currentNode = nextNode;


// Remove field end.





// Save output.


// ----------==============STAGE 2==============----------

// Open output find bookmarks, remove everything from bookmarks and insert mergefields.

Document doc1 = new Document(@"Test001\out1.docx");

DocumentBuilder builder = new DocumentBuilder(doc1);

// Regular expression will be used to get mergefield name form the bookmark name.

Regex regex = new Regex("[^_\\d]*");

foreach (Bookmark bookmark in doc1.Range.Bookmarks)


Match match = regex.Match(bookmark.Name);

string fieldName = match.Value;

// Check whether bookmark name is in the list of mergefields.

if (!mergeFieldNames.Contains(fieldName))


bookmark.Text = "";

// Move DocumentBuidler cursor to the bookmarks and insert a mergefield.

builder.MoveToBookmark(bookmark.Name, true, true);

builder.InsertField(string.Format("MERGEFIELD {0}", fieldName));


// Save document.


// ----------==============STAGE 3==============----------

// Open newly created tempalte and execute mail merge.

Document doc2 = new Document(@"Test001\out2.docx");

// Execute mail merge.

doc2.MailMerge.Execute(names, names);

// Save output.


Hope this helps.

Best regards,

Thanks for finding the strange fields. I will try out the code and let you know how it goes.