Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

How to find a bookmark's parent bookmarks?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew Sharpe - 15 Sep 2005 20:40 GMT
Given an instance of Microsoft.Office.Tools.Word.Bookmark, is there a way to
determine any surrounding bookmarks?  I want to know all the bookmarks that
contain the range of a given bookmark.

Thoughts?
Helmut Weber - 15 Sep 2005 21:46 GMT
Hi Andrew,

like this, if I get You right:

Sub Test0023()
Dim oRng As Range ' range of given bookmark
Dim rTmp As Range ' range of bookmarks that contain oRng
Set rTmp = Selection.Range
Set oRng = ActiveDocument.Bookmarks("Mark1").Range
rTmp.SetRange oRng.Bookmarks(1).Start, _
oRng.Bookmarks(oRng.Bookmarks.Count).End
rTmp.Select
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Jay Freedman - 15 Sep 2005 22:19 GMT
> Given an instance of Microsoft.Office.Tools.Word.Bookmark, is there a
> way to determine any surrounding bookmarks?  I want to know all the
> bookmarks that contain the range of a given bookmark.
>
> Thoughts?

Use the InRange function like this, as you iterate through the Bookmarks
collection:

  Dim firstBk As Bookmark, tryBk As Bookmark

  Set firstBk = ActiveDocument.Bookmarks("bk3")

  For Each tryBk In ActiveDocument.Bookmarks
     If firstBk.Range.InRange(tryBk.Range) _
           And (firstBk.Name <> tryBk.Name) Then
        MsgBox firstBk.Name & " is inside " & tryBk.Name
     End If
  Next tryBk

Note that bookmarks that only partly overlap won't pass the test. That is,
if you have the sentence "The quick brown fox jumps over the lazy dog",
where bk1 covers "quick brown fox" and bk3 covers "fox jumps", then bk3
won't be considered to be inside bk1.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Andrew Sharpe - 16 Sep 2005 13:23 GMT
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.

> > Given an instance of Microsoft.Office.Tools.Word.Bookmark, is there a
> > way to determine any surrounding bookmarks?  I want to know all the
[quoted text clipped - 20 lines]
> where bk1 covers "quick brown fox" and bk3 covers "fox jumps", then bk3
> won't be considered to be inside bk1.
Jay Freedman - 16 Sep 2005 16:12 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.