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 / April 2005

Tip: Looking for answers? Try searching our database.

Befuddled by Bookmarks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TT - 02 Apr 2005 21:47 GMT
I am really becoming confused when it comes to navigating bookmarks. I have
no problem when I reference bookmarks by name, but I run into problems
whenever I try to vavigate based on wdGotoNext, wdGotoAbsolute, wdGotoFirst,
etc. I have a document with 3 bookmarks (the msgbox confirms this when I run
the macro below). However, the code below always returns "The bookmark does
not exist". when it hits the .Goto. What am I missing?

Public Sub macro2()
Dim rng As Range
MsgBox ActiveDocument.Range.Bookmarks.Count
Set rng = ActiveDocument.Range.GoTo(wdGoToBookmark, wdGoToAbsolute, 2)
End Sub
Jay Freedman - 03 Apr 2005 02:59 GMT
>I am really becoming confused when it comes to navigating bookmarks. I have
>no problem when I reference bookmarks by name, but I run into problems
[quoted text clipped - 8 lines]
> Set rng = ActiveDocument.Range.GoTo(wdGoToBookmark, wdGoToAbsolute, 2)
>End Sub

I think the GoTo method is semi-broken, but I never use it for this
kind of application anyway. This statement does the same thing and is
100% reliable:

  Set rng = ActiveDocument.Range.Bookmarks(2).Range

Technically, if the GoTo method worked as documented, it would return
"that represents the start position of the specified item", so you'd
have to follow the Set statement with

  rng.Collapse Direction:=wdCollapseStart

to get the same result, but if you really wanted the range of the
entire bookmark then you can omit it.

There is a small wrinkle to this, which you may want to take advantage
of someday... The expression ActiveDocument.Range.Bookmarks(2).Range
returns the range of the second bookmark *by physical location in the
document*, while ActiveDocument.Bookmarks(2).Range returns the range
of the second bookmark *alphabetically by name in the list of
bookmarks*. This difference corresponds to the two sorting orders you
can see in the Insert > Bookmark dialog when you click the "Name" and
"Location" radio buttons.

--
Regards,
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.