Thanks Jay. I was hoping there was an easier way then iterating through all
the bookmarks and checking InRange, because I need to know the hierarchy of
them as well, ie I need to know if A contains B, C contains A, etc. So I
will need to "sort" them afterward. But your suggestion will definitely
work, so thanks a lot for it.
Hi Andrew,
Actually, you can reduce the number of candidates by iterating through
firstBk.Range.Bookmarks instead of ActiveDocument.Bookmarks:
For Each tryBk In firstBk.Range.Bookmarks
That will test only the bookmarks that have some part of their range in
firstBk.Range (either enclosing it, inside it, or overlapping it), but it
will ignore any bookmarks that don't touch firstBk. If you have a lot of
bookmarks, that'll reduce the work considerably.
As for "sorting" the hierarchy, once you have the list of enclosing
bookmarks you should be able to sort them in order of their .Start values.

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
> Thanks Jay. I was hoping there was an easier way then iterating
> through all the bookmarks and checking InRange, because I need to
[quoted text clipped - 31 lines]
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
Andrew Sharpe - 16 Sep 2005 19:50 GMT
Good stuff Jay. If I were looking for all the bookmarks "contained" within
a given bookmark that would work perfectly. But what I actually want to do
is look for all bookmarks that "contain" a given bookmark. So I think I
still have to iterate through all bookmarks. But yeah good point about
using Range.Start to sort them. That's better.
Another somewhat related problem is given a bookmark, 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
Thank you SO MUCH for your continued efforts. They are MUCH appreciated.
Andrew
> Hi Andrew,
>
[quoted text clipped - 46 lines]
> >> Jay Freedman
> >> Microsoft Word MVP FAQ: http://word.mvps.org
Jay Freedman - 16 Sep 2005 20:51 GMT
Hi Andrew,
Actually, iterating through firstBk.Range.Bookmarks will give you both the
bookmarks that contain firstBk and the ones it contains, and the expression
firstBk.Range.InRange(tryBk.Range) will return True only when firstBk is
inside (or equal to) tryBk. That's exactly the relation you want.
I've never worked with code like yours -- is that VB.Net? -- so I can't help
with the problem. You might have a better chance of getting answers in one
of the microsoft.public.office.developer newsgroups.

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
> Good stuff Jay. If I were looking for all the bookmarks "contained"
> within a given bookmark that would work perfectly. But what I
[quoted text clipped - 105 lines]
>>>> Jay Freedman
>>>> Microsoft Word MVP FAQ: http://word.mvps.org