Overlapping Bookmarks

Bookmarks can overlap. For example, text in a document can be shared between bookmarks. Figure 6.22 shows two overlapping bookmarks. The first one is displayed within light gray brackets, and the second one is displayed within black brackets.

This is some sample [text to show howbookmarks] can overlap. Figure 6.22. Overlapping bookmarks

In this example, Bookmarkl contains the text "This is some sample text to show," Bookmark2 contains "text to show how bookmarks," and the text in the overlap of the bookmarks is "text to show."

When you write code to handle a bookmark event, you need to make sure that you are handling the event of the correct bookmark. For example, if you have code that handles the double-click event of all the bookmarks in your document, and if a user double-clicks on the word show, which bookmark's event should be handled? By default, each bookmark would raise this event and the shared event handler would be called twice.

You can write code to check for overlapping bookmarks and determine which one should handle the event. One approach is to check whether the current selection contains more than one bookmark by checking the Count property of the bookmark (Me.Application.Selection.Book-marks.Count). If it is greater than 1, you then check which bookmark is closer to the selection and handle only its event.

Assignment also behaves differently in overlapping bookmarks. If you assign new text to Bookmark1, such as "This is text," what would you expect the text of Bookmark1, Bookmark2, and the overlap to be? Using VSTO, Bookmark1 would contain the new text you assigned ("This is text"), and Bookmark2 would contain only the text that did not appear in the original Bookmark1 ("how bookmarks"). The bookmarks would no longer overlap. You should keep this in mind when you assign text to bookmarks. It might be a good idea to check whether there are any overlapping bookmarks before you assign new text to a bookmark.

0 0

Post a comment