Bookmark Menu

J

jerem

I've copied the code below and tried to use it with a bookmarked document.
When I run the macro a bookmark menu does not appear in the document. I get
no runtime errors or anything like that. It just doesn't produce anything. I
stepped through some of the code and it seems that it does recognize that
there are 4 bookmarks at this portion of code (With oButton, .Caption =
oBookmark.Name, .Style = msoButtonCaption, .OnAction = "BookMarkSelect",
End With, Next) but when it hits the "Next" statement in the previous
parenthetical code the popup value (or whatever you call the message that
tells you what the value is at that point) says the value of .OnAction =
<Object variable or With Block variable not set>. Can anybody tell me why
the Object variable is not being set and what's going wrong here?

Also, while stepping through it it never enters the code below I guess
because the object variable is not set???:

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

Here is the entire code that I copied from the MVP website:

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

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

Thanks for your help.
 
D

Doug Robbins - Word MVP

What version of Word are you using?

If it is Word 2007, that no longer supports custom toolbars, after you run
the first code, you will find a Bookmarks drop down in the Menu Commands
section of the Add-Ins tab of the ribbon.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

jerem

I'm using Word 2002


Doug Robbins - Word MVP said:
What version of Word are you using?

If it is Word 2007, that no longer supports custom toolbars, after you run
the first code, you will find a Bookmarks drop down in the Menu Commands
section of the Add-Ins tab of the ribbon.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


.
 
D

Doug Robbins - Word MVP

In that version of Word, a Bookmarks menu item will appear to the right of
the Help menu.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

jerem

Hey Doug,

Didn't think to look there, but you're right - there it was. Just one
question - I already had an AutoOpen macro that does something else. When I
copied the code in for the BookmarkMenu, it too had an AutoOpen which when
run gave me a message of Ambiguous something or other. So, I had to comment
out the other AutoOpen in order to get this code to work. That's fine,
however, when I went to the internet to learn more about AutoOpen I got this:

"AutoOpen
The AutoOpen macro runs after you open a new document. AutoOpen runs when
you open a document in the following ways:
• Use the Open command on the File menu.
• Use the FileOpen or FileFind commands.
• Select a document from the Most Recently Used (MRU) list on the File menu.
When a document is opened, an AutoOpen macro runs if the AutoOpen macro is
saved as part of that document or if the macro is saved as part of the
template on which the document is based."

The previous AutoOpen and the one in this code is run as part of the
template on which the document is based. What do you do when you need both
AutoOpens. Name one AutoOpen1, AutoOpen2 or , more particularly, what if
you want one AutoOpen to only apply to a certain set of code and the other to
apply to another set of code or should these AutoOpens not be called AutoOpen
and just be a macro that is called from another macro??????

Thanks for your help.
 
D

Doug Robbins - Word MVP

Combine both lots of code into the one AutoOpen macro.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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