Word

Check for Bookmark Name With Custom Right-Click Menu

Ease of Use

Easy

Version tested with

2000, 2003 

Submitted by:

MOS MASTER

Description:

Programmatically add button to right click menu. The button code shows all bookmark names in current selection. 

Discussion:

An easy way to get the bookmark names in the selection via the context menu. The idea is to provide a button on witch you can attach a procedure you will often make use of. 

Code:

instructions for use

			

Option Explicit Private Sub Document_Open() Dim oButton As Office.CommandBarControl CustomizationContext = NormalTemplate 'Make changes to Normal.dot 'Add button to right-click menu Set oButton = Application.CommandBars("Text").Controls.Add 'Set properties of button With oButton .Caption = "Show Bookmarks" .FaceId = 351 .Style = msoButtonIconAndCaption .OnAction = "ThisDocument.ShowBookmarksInSelection" .BeginGroup = True End With 'Clean up Set oButton = Nothing End Sub Private Sub Document_Close() On Error Resume Next CustomizationContext = NormalTemplate 'Make changes to Normal.dot 'Delete button on right-click menu Application.CommandBars("Text").Controls("Show Bookmarks").Delete End Sub Sub ShowBookmarksInSelection() Dim oRange As Word.Range Dim oBm As Word.Bookmark Dim sMsg As String Set oRange = Selection.Range For Each oBm In oRange.Bookmarks 'loop trough bookmarks in range sMsg = sMsg & oBm.Name & vbCrLf 'build string off bookmark names Next 'If any If sMsg <> "" Then MsgBox sMsg 'show bookmark names End If 'Clean up Set oRange = Nothing End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open the VBE window.
  3. Double click "ThisDocument" on the project explorer. (press CTRL+R to view)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. Double click the file to open and the button is added to the context menu
  2. Select some text that?s bookmarked and click button ?Show Bookmarks? on the context menu
  3. A message box will show you all the bookmark names. (in selection)
 

Sample File:

Add button to (right-click) context menu.zip 8.77KB 

Approved by mdmackillop


This entry has been viewed 82 times.

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