I am trying to replace some code that currently uses Word Automation, and have been testing Aspose.Words for .Net. Part of the existing application utilises Mail Merge, and I have been able to replicate that piece using Aspose.Words. However, before the Mail Merge runs, there is also some code which loops through all of the Mail Merge fields, tests to see whether or not they exist within the Mail Merge database table, and if not it searches some other database tables, and (if it finds the field names in these database tables) it replaces the Mail Merge field with the database value. It's like a manual pre-merge before the proper merge. I know this probably sounds a bit odd, but it does make sense within the context of the existing application!
I have been able to write the Aspose.Words code to a point whereby the Mail Merge is working, and the other code which looks at additional database tables is also almost working. However, I have run into a problem that I need some advice on.
In order to loop around the Mail Merge fields, I have written the following code:
Dim Collection As Aspose.Words.NodeCollection
Dim start As Aspose.Words.FieldStart
Dim CurrentNode As Aspose.Words.Node
Collection = TableCell.GetChildNodes(Aspose.Words.NodeType.FieldStart, True)
For Each start In Collection
If start.FieldType = Aspose.Words.FieldType.FieldMergeField Then
CurrentNode = start
While CurrentNode.NodeType <> Aspose.Words.NodeType.FieldSeparator
CurrentNode = CurrentNode.NextSibling
If CurrentNode.NodeType = Aspose.Words.NodeType.Run Then FieldName = FieldName + CurrentNode.ToTxt
FieldName = FieldName.Replace("MERGEFIELD", "") 'remove "MERGEFIELD" from FieldName
FieldName = FieldName.Replace("""", "") 'remove quotes from FieldName
If FieldName.IndexOf("\") > 0 Then FieldName = FieldName.Substring(0, FieldName.IndexOf("\")).Trim 'remove any trailing items (Word formatting) from FieldName
This allows me to loop through each Merge Field in turn, and is working correctly. I have then written some code that looks at the FieldName value, and if it is a field that needs to be replaced, calls a routine which (1) steps back through each PreviousSibling of CurrentNode until it finds FieldStart, and then (b) removes each NextSibling until it reaches FieldEnd. This allows the whole Field to be deleted - and this too is working correctly.
The problem I am having is that in the first piece of code, the line "For Each start In Collection" is getting confused. Let's say that the first time the For loop is executed it finds 2 Mail Merge fields. The first time through the loop the first Mail Merge field will be replaced correctly. However, when the code gets to the Next statement, there is now only 1 Mail Merge node, and so the loop exits (without processing the second Mail Merge field).
I have tried re-writing this code several different ways, but I'm wondering whether I am missing something. Is there an easier way to loop through each of the Mail Merge fields, and replace those that need replacing with plain text?