How to get all other Bookmarks?

A

Andrew Sharpe

Given a Tools.Word.Bookmark object, how do I get a reference to the master
collection of bookmarks (need the new
Tools.Word.Bookmark objects as opposed to the Interop.Word.Bookmark
objects)? The Container property looked promising, in the Immediate window
it returned an instance of ThisDocument, which is perfect because it has the
Controls property that I can use. But anytime I try to reference the
Controls property in code, it returns a Null Reference error. The API docs
say that this will be the case if the object returned does not implement
IContainer, which ThisDocument of course does not. Have a look, let me know
what you think. Note that the Bookmark class refers to the new
Tools.Word.Bookmark version (which i require).

Public Shared Function GetSurroundingBookmarks(ByVal bMark As Bookmark)
As List(Of Bookmark)
Dim surBMarks As New List(Of Bookmark) ' surrounding bookmarks

Dim doc As ThisDocument = DirectCast(bMark.Container, ThisDocument)
' doc is Nothing, despite the fact that evaluating bMark.Container
in the Watch or Immediate windows clearly show it returning an instance of
ThisDocument
Dim all As ControlCollection = doc.Controls

For Each obj As Object In all
If TypeOf obj Is Bookmark Then
Dim curBMark As Bookmark = DirectCast(obj, Bookmark)
If bMark.InRange(curBMark.Range) Then
Dim i As Integer
For index As Integer = 0 To surBMarks.Count - 1
If surBMarks(index).InRange(curBMark.Range) Then
i = index
End If
Next
surBMarks.Insert(i, curBMark)
End If
End If
Next

Return surBMarks
End Function
 
Top