Word / OpenXML - How to create a hidden bookmark?

I was not lucky either in the OpenXML API or in the Word / VSTO API that you found a way to create or change the visibility of bookmarks. Even when manually adding bookmarks to Word, there is no window to check to make the bookmark hidden. Although the Bookmarks dialog box has a checkbox that allows you to show hidden bookmarks. So, how are hidden bookmarks represented in XML, and can you create them using the Open XML SDK? Or is it an obsolete thing that MS no longer wants to create?

+6
ms-word vsto openxml
source share
2 answers

OK, so it's easier than I thought ... you just precede the bookmark name with an underscore. Please note that this can only be done programmatically, and not by adding bookmarks manually in Word.

Iiiiiiiiinteresting ....

Update: Another thing I found is that before you can iterate or access hidden bookmarks in a Bookmarks object, you must set the ShowHidden property to true.

PS - SO, if you have any control over your spelling dictionary, you can add "programmatically". If I'm not mistaken.:)

+12
source share

I created regular bookmarks in a text file and converted them to a hidden bookmark pragmatically. As mentioned above, hidden bookmarks can only be created pragmatically, and their name is preceded by "_". Whenever a bookmark list is ever repeated, make sure Bookmarks.ShowHidden is set to true, otherwise hidden bookmarks will not appear in the list. Below is the code I used to hide all visible bookmarks. At the very end, I also clear the undo entry to make sure that the user cannot undo the changes I made. You can create a custom cancellation record, if you want, delete the last action.

public static void hideAllBookmark(Document doc) { String newName = null; Range newRange = null; bool backup = doc.Bookmarks.ShowHidden; doc.Bookmarks.ShowHidden = false; for (int i = doc.Bookmarks.Count; i > 0; i--) { if (!doc.Bookmarks[i].Name.Substring(0, 1).Equals("_", StringComparison.OrdinalIgnoreCase)) { newName= "_" + doc.Bookmarks[i].Name; newRange = doc.Bookmarks[i].Range; doc.Bookmarks[i].Delete(); doc.Bookmarks.Add(newName, newRange); } } doc.Bookmarks.ShowHidden = backup; doc.UndoClear(); } 
0
source share

All Articles