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
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