Create Bookmarks Menu HELP

J

Jake Cole

I found the following code on the www.mvp.org/word site. It creates a
bookmarks menu with all of the bookmarks in a current document and
allows you to click on them and move through the document. This is a
very nice feature for working with large word documents. Here is the
code:

Option Explicit

Sub CreateBookMarkMenu()

Dim oBookmark As Bookmark
Dim oBar As CommandBar
Dim oPopup As CommandBarPopup
Dim oButton As CommandBarButton
Dim ShowHiddenStatus As Boolean

'Find out whether hidden bookmarks set as "visible" or not,
'storing this setting in a variable so it can be returned to at the
end.
'Then make the hidden bookmarks invisible
'(we don't want cross-refs etc to appear in our menu)
ShowHiddenStatus = ActiveDocument.Bookmarks.ShowHidden
ActiveDocument.Bookmarks.ShowHidden = False
ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation
CustomizationContext = ActiveDocument
Set oBar = CommandBars.ActiveMenuBar

'First delete Bookmark menu if it already exists
Set oPopup = CommandBars.FindControl(Tag:="Recreate")
If Not oPopup Is Nothing Then
oPopup.Delete
End If


If ActiveDocument.Bookmarks.Count > 0 Then

Set oPopup = oBar.Controls.Add(Type:=msoControlPopup, _
Before:=oBar.Controls.Count + 1)

With oPopup
.Caption = "Bookmarks"
.Tag = "Recreate"
End With

For Each oBookmark In ActiveDocument.Bookmarks
Set oButton = oPopup.Controls.Add(Type:=msoControlButton)

With oButton
ActiveDocument.Bookmarks.DefaultSorting =
wdSortByLocation
.Caption = oBookmark.Name
.Style = msoButtonCaption
.OnAction = "BookMarkSelect"
End With

Next

'Add a Refresh button at the bottom
Set oButton = oPopup.Controls.Add(Type:=msoControlButton)

With oButton
.Caption = "Refresh list"
.Style = msoButtonCaption
.OnAction = "CreateBookMarkMenu"
.BeginGroup = True
End With

End If

ActiveDocument.Bookmarks.ShowHidden = ShowHiddenStatus

Set oButton = Nothing
Set oPopup = Nothing
Set oBar = Nothing
Set oBookmark = Nothing

End Sub

Private Sub BookMarkSelect()
If ActiveDocument.Bookmarks.Exists(CommandBars.ActionControl.Caption)
Then
ActiveDocument.Bookmarks(CommandBars.ActionControl.Caption). _
Range.Select
End If
End Sub

Sub AutoOpen()
'Make sure the document's menu is visible when the document opens
'If the "customisation context" has been changed since it was last
opened,
'the document-specific menus won't be visible!
CustomizationContext = ActiveDocument
End Sub


I have to problems, I would like the bookmarks listed in my new menu
to be listed in the order they occur in the document and not in
alphabetical order as they are when you run this macro. How do I do
that?

Also is there anyway to change the text of the menu items so I do not
have to them all run together or have underscores?

Example I want my bookmark menu items to read like this "My Bookmark"
not "My_Bookmark" as they are now, but still maintain the button's
functionality? I realize I might have to write code for each
individual entry, I am willing to do that. Could you please give me
something to get started.

Thanks!

Jake
 
C

Cindy Meister -WordMVP-

Hi Jake,
I found the following code on the www.mvp.org/word site.
Note that you're more likely to find the person who "owns" this in one
of the word.vba groups. It's the first time I've seen the macro, but I
might be able to point you in the right direction...
I would like the bookmarks listed in my new menu
to be listed in the order they occur in the document and not in
alphabetical order as they are when you run this macro. How do I do
that?
Change the following line
For Each oBookmark In ActiveDocument.Bookmarks
to
For Each oBookmark In ActiveDocument.Range.Bookmarks
Example I want my bookmark menu items to read like this "My Bookmark"
not "My_Bookmark" as they are now, but still maintain the button's
functionality?
You'd need to run the VB Replace function on the name, both when
building the menu and jumping to the bookmark. (Note: I'm assuming you
have Word 2000 or later!)

Very roughly:
Sub CreateBookMarkMenu()

'Insert in correct place
.Caption = Replace(oBookmark.Name, "_", " ")
.Style = msoButtonCaption


Private Sub BookMarkSelect()
Dim szBkmName as String
szBkmName = Replace(CommandBars.ActionControl.Caption, _
" ", "_")
If ActiveDocument.Bookmarks.Exists(szBkmName)
Then
ActiveDocument.Bookmarks(szBkmName). _
Range.Select
End If
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top