We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Search and replace in a bookmark

Hi,
I am looking for the beste way to search and replace within a bookmark. I would like to retrieve the contents of a bookmark and replace multiple words (if present). The replaced content is bookmark dependent, so I can't do a document wide find/replace.

[Your score for <scale> is <average>.]
[Your score for <scale> is <average>.]

If the above are two different bookmarks, the result could look like:
[Your score for depression is high.]
[Your score for stress is low.]

I am using bookmarks, because depending on business conditions, some bookmark text will be deleted all together.

I did not find a way to do Find and Replace in a TextBodyPart.
The only way I have found is to get the content of the bookmark as a WordDocument, then to the Find and Replace, but then I struggle to replace the contents of the bookmark with the processed WordDocument. It also does not sound as a very performant way, but I don't know that for sure.

Can you set me in the right direction?

Thanks

Pieter

2 Replies

MJ Mohanaselvam Jothi Syncfusion Team July 1, 2019 12:28 PM UTC

Hi Pieter,

Thank you for contacting Syncfusion support.

To achieve your requirement, we suggest you get the bookmark contents and replace the text within the bookmark contents. We have prepared a sample application for the same and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/145596/ze/Sample_appplication20057606

In this sample, we have done the following things:
  1. Loads an template Word document.
  2. Get the bookmark content using BookmarksNavigator API.
  3. Iterate into bookmark contents and replace the text.
  4. Replace the bookmark content.
  5. Saves the Word document.

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



PV Pieter van Kampen July 2, 2019 08:02 AM UTC

This is great! Iterating through the bookmark content is exactly what I was looking for.

Many thanks,
Pieter

Loader.
Live Chat Icon For mobile
Up arrow icon