check if a bookmark exists

Hi,

How can I check if a bookmark (still) exists in a document?


honestly I want to delete bookmark content, but this is only possible if the bookmark exists otherwise the code stops with an error (System.NullReferenceException).


Please help me

regards
Martin




2 Replies 1 reply marked as answer

LB Lokesh Baskar Syncfusion Team March 31, 2022 11:38 AM UTC

Hi Martin ,

From the details, we have found that your requirement is to check whether the required bookmark is present in the Word document. You can achieve this requirement by getting the bookmark instance using FindByName API in BookmarkCollection. We have prepared the sample application as per requirement. Please find the sample from the below link:


In the above sample, we have done the following things:
 Open the word document
 Check Word document has bookmarks or not using bookmark collection.
 Then, get the bookmark instance using the FindByName API in BookmarkCollection with the bookmark name
 And then, check the bookmark is not null.
 If not null, delete the content of the current bookmark.
 Saves the Word document

Please find the below links to know how to work with bookmarks using DocIO
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#obtaining-a-bookmark-instance
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#deleting-content-from-a-bookmark

Regards,

Lokesh B


Attachment: FindBookmark_733ccd36.zip

Marked as answer

MG Martin Goetz April 1, 2022 01:34 PM UTC

Hi,
thank you very much. Your code works fine. But it looks like there was a malformed word document on my system. I needed to delete all my bookmarks and add them manually again. Now it works.

regards

martin


Loader.
Up arrow icon