PreviousBookmarkId and get_item give unexpected result RRS feed

  • Question

  • The following code produces a previous bookmark id of 45 but the bookmarks.count property is only 44.  The get_item method fails with "index not in collection".  Am I missing something here?

     if (this.Application.Selection.Range.PreviousBookmarkID > 0)


                            object paramValue = this.Application.Selection.Range.PreviousBookmarkID;

    //following statement fails

                            bmk = this.Application.ActiveDocument.Bookmarks.get_Item(ref paramValue);

                            if (ObjectModel.Steps.GetStepIndexByID(bmk.Name) > 0)

                                ObjectModel.Steps.CurrentStepIndex = ObjectModel.Steps.GetStepIndexByID(bmk.Name);





    Friday, August 26, 2011 7:54 PM


  • Hi Eddie

    I suspect the problem comes from using Range.Bookmarks on the one hand and ActiveDocument.Bookmarks on the other. The collections are sorted differently. Range.Bookmark sorts by position in the Range, ActiveDocument.Bookmark sorts bookmarks alphabetically.

    So Selection.Range.PreviousBookmarkID is not necessarily going to correspond to ActiveDocument.Bookmarks(index). It might be better to use ActiveDocument.Content.Bookmarks(index). The Content property of the Document object returns a RANGE, so the sort order should correspond

    Cindy Meister, VSTO/Word MVP
    Saturday, August 27, 2011 7:30 AM