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

Free Support Forum - aspose.com

MailMerge with bookmarks

I have a MS Word template that has the following logic:


{ SET BOOKMARK { MERGESEQ }}

Value of BOOKMARK: { REF BOOKMARK}

Value of MERGESEQ: { MERGESEQ }

When I run the merge through MS Word, I get the “value of” BOOKMARK and MERGESEQ to be the counter of the records, as I would expect. However, we I run this through Aspose.Words I get don’t get a value for the book mark. Here is a snippet of the code that I am running:

Dim doc As New Document(wordTemplate)
doc.MailMerge.Execute(dt)

FieldsHelper.ConvertFieldsToStaticText(doc, Fields.FieldType.FieldMergeSeq)
FieldsHelper.ConvertFieldsToStaticText(doc, Fields.FieldType.FieldSet)
FieldsHelper.ConvertFieldsToStaticText(doc, Fields.FieldType.FieldRef)

doc.Save(stream, SaveFormat.Docx)

Thanks - Ken

Hi,


Thanks for your inquiry. Unfortunately, it is difficult to say what the problem is without the template document. I need this Word template document to reproduce the problem on my side.

It is safe to attach files in the forum. If you attach your document here, only you and Aspose staff members can download it. Also you can send the file to my e-mail as described here:
http://www.aspose.com/corporate/purchase/faqs/send-license-to-aspose-staff.aspx

You can also remove any sensitive information by replacing it with dummy data instead.

Best regards,

I just updated my original post with my sample Word template.

Hi,


Thanks for your inquiry. In this case, probably you should just call Document.UpdateFields method after calling MailMerge.Execute e.g:

Dim doc As
New Document(wordTemplate)
doc.MailMerge.Execute(dt)

doc.UpdateFields()

FieldsHelper.ConvertFieldsToStaticText(doc,
Fields.
FieldType.FieldMergeSeq)
FieldsHelper.ConvertFieldsToStaticText(doc,
Fields.
FieldType.FieldSet)
FieldsHelper.ConvertFieldsToStaticText(doc,
Fields.
FieldType.FieldRef)

doc.Save(stream, SaveFormat.Docx)

For more information about Document.UpdateFields, please visit the following link:
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.document.updatefields.html

I hope,this will help.

Best Regards,

Thanks this fixed part of my problem, but when I enhanced my template, it became apparent that the way the Word merge works is that the code to convert to static is a post process, rather being executed inline during the MailMerge.Execute.


I have attached an enhanced version of the MailMergeFormFields demo solution.

In the solution, if you run the process, you will see in the WordMerge.docx output file that I set the COMPARE2 bookmark (COMPARE2A), if I read the bookmark reference immediately following the assignment that the value is different than what I would expect. It appears that the the values are actually being assigned during the ConvertFieldsToStaticText method, so my bookmarks are getting set to the last assignment to that bookmark.

Here is an example from Microsoft that I am trying to recreate:

http://support.microsoft.com/kb/211303

Ken

I have decided to take an alternate and probably easier approach and to use the MailMerge for regions. I will reconfigure my code to split my dataset into multiple data tables and build the relationship between the tables.


Thanks

Hi,


Thanks for the additional information. Please note that ConvertFieldsToStaticText method does not have anything to do with the actual MailMerge process. Its purpose is to remove dynamic fields from a document which change the text they display when updated and transform them into plain text that will remain as they are even when fields are updated. For more information about ConvertFieldsToStaticText method, please visit the following link:
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-replace-fields-with-static-text.html

Please let us know if you need more information, we are always glad to help you.

Best Regards,

Hi there,


Awais is indeed correct with his suggestions. In addition, I would highly suggest you to use nested mail merge regions instead as it would be far easier to merge such data into your document using this technique.

I only took a quick look at your reference site, but from the looks of it that arrangement will only work if your data source is sorted and continuous. This may cause you problems down the line which is why I suggested using nested mail merge instead.

Thanks,