Search code examples
c#ms-wordbookmarks

Get All Bookmarks from Word Document


I've been looking around for a solution to this for a little while now.

I need to extract all bookmarks from a word document so I can manipulate them as I please. So I guess I just need a reference to them rather than the actual bookmarks.

What I've found so far is just solutions where you need to search for names of the bookmarks but I just want to get references to all the bookmarks. I might not know their names at all times. I thought that I could get the bookmarks by calling MyDocument.Bookmarks but so far I still haven't figured out how to work that property.

If I extract the bookmarks from a template, the names won't change. But if I take the bookmarks from an unknown document, I have no idea how to get the references.

Any help?


Solution

  • foreach (Bookmark bookmark in document.Bookmarks)
    {
        // Do whatever you want with the bookmark.
    }