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

Free Support Forum - aspose.com

Iterate over merge fields

Hello,

I’m having a problem with merge fields again… I am using DocumentBuilder.MoveToMergeField to iterate to each merge field in the collection and then perform some actions on them. I just noticed that when I have more than one instance of the same merge field (the same names) then my code doesn’t work.
MoveToMergeField seems to always move to the first occurrence of given field.
Is there any other way to iterate over merge fields ? I didn’t find anything useful in the documentation and the only thing the comes to my mind is to iterate entire document object model and then check for merge fields manually, but how do I do even that in a reliable way, so that I’m sure I won’t miss anything ?

Best regards,
Ireneusz Patalas

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. May I ask you, why do not you use MailMerge.Execute() as described here:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-execute-simple-mail-merge.html

Best regards,

Hello,



it’s because of our requirements. We need to only replace the displayed value of the merge fields in the documents, keeping the merge fields
information in the document, so that the data can be refreshed later.
MailMerge.Execute() deletes this data, so we decided to do it manually. You can have a look here as well to see what I mean. We are aware that this method is not the best, because updating the fields in such a document will fail, but it’s mainly for read-only documents that sometimes need to be refreshed, so it’s not an issue. We’ve been using such approach with success for months with Word Automation, so it’s not a subject to change now. We just had problems with Word Automation and decided to use a component capable of doing the same.

Best regards,
Ireneusz Patalas

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Maybe, in your case, you can try consider the approach suggested here to iterate through all merge fields in the document:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-rename-merge-fields.html

Hope this helps.

Best regards,

Hello,

I think that’s exactly what I need, I missed that function. Thank you.

Best regards,
Ireneusz Patalas