|
|
|
|
|
|
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()
Dim objBar As CommandBar
Dim objList As CommandBarComboBox
Dim objGoTo As CommandBarButton
Dim objRefresh As CommandBarButton
On Error Resume Next
Set objBar = Application.CommandBars("myBookmarks")
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
RefreshList
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()
Dim objBar As CommandBar
Dim objList As CommandBarComboBox
Dim bmk As Bookmark
Dim i As Integer
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()
Dim objBar As CommandBar
Dim objList As CommandBarComboBox
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:
|
- Copy the code above.
- Open MS Word
- Press [Alt-F11] to open the Visual Basic Editor (VBE).
- Select the "Normal" project on the left (this makes the macro available in all documents)
- Select Insert, Module
- Paste the code into the new code window on the right
- Save the file and close the VBE
|
Test the code:
|
- Create a Word document
- At several points in the document, select some text and change the text colour to red
- With the text still highlighted, select Insert, Bookmark and give it a name
- Select Tools, Macro, Macros
- Double-click on "BookmarkToolbar"
- Select a bookmark from the list
- Click on the "Select" button - the bookmark text will be highlighted in the document
- Create a new bookmark using the steps above
- 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.
|
|