I’m currently evaluating your component and I have an issue I don’t know how to handle. We have a mechanism that replaces mail merge fields with provided data, but currently it’s done using Word Automation and we now want to switch to a more reliable and platform independent solution.
The problem is that after calling MailMerge.Execute() all the fields that has been replaced are lost and after saving such a document they are lost unrecoverably… This is not the case in Word Automation, we need them after save, so that after some time we can update the document with new (updated) values. Is it possible to do it with your component ?
It happens on every document with merge fields, no matter if I take one from our database or create a new one from scratch.
Thanks for your request. If you would like to fill the document with data and then fill the same document with other data, it is impossible to achieve using mail merge. This is how mail merge works: during mail merge, merge fields are replaced with values. So in final document, merge fields are no longer available. In MS Word, this works exactly the same.
If you need to fill document with data several times, you can consider using bookmarks or form fields. Or you can create the template document and use this template each time when you need to perform mailmerge.
thank you for the answer, but it actually didn’t satisfy me. I am evaluating 2 other components that do more or less the same in parallel and one of them provides such a feature. I just wanted to stick with yours, because it has some features we will very likely find useful in near future.
The other component however doesn’t use “Execute” approach, but it rather traverses the document tree and places the text in proper nodes.
I tried the same with Aspose.Words and it seems to work. Here is the code snippet for it:
Dim word As New Aspose.Words.Document(path)
Dim fieldNames() = word.MailMerge.GetFieldNames()
Dim doc As New Aspose.Words.DocumentBuilder(word)
For Each Field As String In fieldNames
If doc.MoveToMergeField(Field, False, False) Then
Dim node As Aspose.Words.Node = doc.CurrentNode
' find FieldSeparator
While node IsNot Nothing AndAlso node.NodeType <> Aspose.Words.NodeType.FieldSeparator
node = node.NextSibling
If node Is Nothing Then Exit For
If node.NextSibling.NodeType = Aspose.Words.NodeType.Run Then
' update existing Run
CType(node.NextSibling, Aspose.Words.Run).Text = FieldValue(Field)
' insert new Dim run As New Aspose.Words.Run(word, FieldValue(Field))
Values inserted like that are displayed like the example values for the merge fields and the merge fields are kept, the results are enough for us. There is only an issue with the formatting. Every new Run node resets the formatting which is not really what we want… It has the Font property which I could use to set the formatting basing on the parent paragraph, but copying it field by field doesn’t seem reasonable for me… isn’t there an easier way ?
Thank you for additional information. I do not think that filling merge fields using such approach is a good idea. In your code you change only displayed text (field value), but if your end-user update field in MS Word all values inserted such way will be lost. Try this on your side (Ctrl+A and then press F9 to update fields).
To preserve Font, try cloning Run form field code (Between FieldStart and FieldEnd) instead of creating new Run from scratch.
Anyways, I think, the best solution for filling merge fields in template with data is using Mail Merge approach as described here:
thank you for pointing that out… I will need to discuss it with project manager, but I think it shouldn’t be a big problem in our case.
Thanks for your request. Please let me know in case of any issues, I will be glad to help you.