Bookmarks in VSTO

Hi everybody,

I had to do some changes in my project, and now I have some problems with the bookmarks. I use this code to change the the text in a bookmark:

  If Me.Bookmarks.Exists("bmName") Then
         Me.Bookmarks.Item("bmName").Range.Text = strText
  End If

Well the text gets replaced but the bookmark disapears. How can I keep the bookmark on the docuemnt

Note: The bookmarks are not added to ThisDocuement.vb. They are added in runtime.


Answer this question

Bookmarks in VSTO

  • Joiko

    Hi Daren,

    I was thinking of something like this already. But I'm not a fan of these kind of solutions. So much code for such a small function. But seems like I have no other possibility. So I'll try it this way. Thanks.

  • ShadabKalim

    Unfortunately, the Bookmark itself is included in the Range returned so when you write text to .Range.Text it removes the Bookmark. If you use the Bookmark object in VSTO2005, they included code to preserve the Bookmark. Alternatively, you can recreate the bookmark yourself using code similar to (apologies for it being in C#):

    private void UpdateBookmarkText(string bookmarkName, string newText)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    {

          object bookmarkNameObject = bookmarkName;

          if (this.Bookmarks.Exists(bookmarkName) == true)

          {

                Word.Bookmark b = this.Bookmarks.get_Item(ref bookmarkNameObject);

                object rangeStart = b.Range.Start;

                object rangeEnd = b.Range.Start + newText.Length;

                b.Range.Text = newText;

                // replace the bookmark we just destroyed

                object range = (object)Range(ref rangeStart, ref rangeEnd);

                this.Bookmarks.Add(bookmarkName, ref range);

          }

    }

     



  • Bookmarks in VSTO