Word

Create floating toolbar listing all document bookmarks

Ease of Use

Intermediate

Version tested with

2000, 2002 

Submitted by:

geekgirlau

Description:

This macro creates a new custom toolbar containing a combo-box listing all bookmarks in the document. The user can go to a bookmark by selecting the name of the bookmark then clicking on a "Select" button. There is also a "Refresh" button to update the list of bookmarks, so the same toolbar may be used for all Word documents. 

Discussion:

This macro is handy for people working with large documents containing lots of bookmarks. It provides a means for quickly jumping to marked positions within your document. 

Code:

instructions for use

			

Sub BookmarkToolbar() '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Purpose: Create a floating toolbar to display a list of all document bookmarks '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dim objBar As CommandBar ' custom toolbar Dim objList As CommandBarComboBox ' list of bookmarks Dim objGoTo As CommandBarButton ' "Select" button Dim objRefresh As CommandBarButton ' "Refresh" button On Error Resume Next Set objBar = Application.CommandBars("myBookmarks") ' error if toolbar does not exist - have to create toolbar ' otherwise make the toolbar visible If Err.Number <> 0 Then Set objBar = CommandBars.Add(Name:="myBookmarks", Position:=msoBarFloating) Set objList = objBar.Controls.Add(Type:=msoControlComboBox, Before:=1) With objList .DropDownLines = 12 .DropDownWidth = 75 .ListHeaderCount = 0 End With ' populate the combo box with list of bookmarks RefreshList ' create command buttons Set objGoTo = objBar.Controls.Add(Type:=msoControlButton) Set objRefresh = objBar.Controls.Add(Type:=msoControlButton) With objRefresh .Style = msoButtonCaption .Caption = "Refresh Bookmarks" .OnAction = "RefreshList" End With With objGoTo .Style = msoButtonCaption .Caption = "Select" .OnAction = "GotoBookmark" End With End If objBar.Visible = True End Sub Sub RefreshList() '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Purpose: Populate the combo box with list of bookmarks '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dim objBar As CommandBar ' custom toolbar Dim objList As CommandBarComboBox ' list of bookmarks Dim bmk As Bookmark ' document bookmarks Dim i As Integer ' counter Set objBar = Application.CommandBars("myBookmarks") Set objList = objBar.Controls(1) i = 1 objList.Clear For Each bmk In ActiveDocument.Range.Bookmarks objList.AddItem Text:=bmk.Name, Index:=i i = i + 1 Next bmk End Sub Sub GotoBookmark() '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Purpose: Go to the selected bookmark '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dim objBar As CommandBar ' custom toolbar Dim objList As CommandBarComboBox ' list of bookmarks Set objBar = Application.CommandBars("myBookmarks") Set objList = objBar.Controls(1) If objList.Text <> "" Then Selection.GoTo What:=wdGoToBookmark, Name:=objList.Text Else MsgBox "Please select the name of the bookmark", vbInformation, "No Bookmark Selected" End If End Sub

How to use:

  1. Copy the code above.
  2. Open MS Word
  3. Press [Alt-F11] to open the Visual Basic Editor (VBE).
  4. Select the "Normal" project on the left (this makes the macro available in all documents)
  5. Select Insert, Module
  6. Paste the code into the new code window on the right
  7. Save the file and close the VBE
 

Test the code:

  1. Create a Word document
  2. At several points in the document, select some text and change the text colour to red
  3. With the text still highlighted, select Insert, Bookmark and give it a name
  4. Select Tools, Macro, Macros
  5. Double-click on "BookmarkToolbar"
  6. Select a bookmark from the list
  7. Click on the "Select" button - the bookmark text will be highlighted in the document
  8. Create a new bookmark using the steps above
  9. Click on the "Refresh" button - the new bookmark will be added to the list on the toolbar
 

Sample File:

BookmarkToolbar.zip 9.92KB 

Approved by mdmackillop


This entry has been viewed 129 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express