Word

A full featured Bookmarks utility

Ease of Use

Easy

Version tested with

2002, 2003 

Submitted by:

fumei

Description:

This form handles most tasks related to bookmarks. You can go to a bookmark, select a bookmark, delete a bookmark and add a bookmark. All existing bookmarks are listed automatically. 

Discussion:

A "floating" UserForm that automatically lists all existing bookmarks. You can go to any bookmark (but do NOT select it), OR go to the bookmarks AND select it. You can delete any bookmark listed. You can create bookmarks from any selected text. Any added bookmarks are automatically refreshed in the list. As the form is not modal, you can work in the document and create bookmarks simply by selecting text and adding a bookmark name. You can freely move the form anywhere, and still work in the document. The form has a Hide button. This resizes the form and places it at the top of the screen. If it is at that location, a single click will resize it fully, and center it. If you have moved the form (and it does not matter where), a second click will return it to the original location. The form can be loaded by Alt-B keyboard shortcut; or it can be loaded by clicking the Bookmarks "icon" on the Standard toolbar - to the right of the Size dropdown. You can of course use any other method of loading the form. Add it to a menu, etc etc. As this is a UserForm, you need to copy it into Normal.dot or any other global template to use for any document. 

Code:

instructions for use

			

Option Explicit ' * * * * * * * * * * * * * * * * * * * ' Initialize and Populating Procedures ' * * * * * * * * * * * * * * * * * * * Private Sub UserForm_Initialize() ' hide the label that makes ' the form bigger (after it has been hidden) lblShowMe.Visible = False ' populates the bookmark dropdown list FillBookmarkList End Sub Sub FillBookmarkList() Dim mBookmark As Bookmark On Error GoTo NoBookmarks ' if there are bookmarks ' add the name of each one to ' the Bookmarks combobox If ActiveDocument.Bookmarks.Count > 0 Then For Each mBookmark In ActiveDocument.Bookmarks() cboBookmarks.AddItem mBookmark.Name Next ' display the first bookmark in the list cboBookmarks.ListIndex = 0 Exit Sub Else GoTo NoBookmarks End If Exit Sub NoBookmarks: ' no bookmarks? display that cboBookmarks.AddItem " no bookmarks " cboBookmarks.ListIndex = 0 End Sub Private Sub cmdGoToBookmark_Click() ' moves the Selection point to ' just before the bookmark (if it exists) ' does NOT select the bookmark If BookmarkExists(cboBookmarks.Text) Then With Selection .GoTo what:=wdGoToBookmark, Name:=cboBookmarks.Text .Collapse direction:=wdCollapseStart End With Else MsgBox "Bookmark doesn't exists" End If End Sub Private Sub cmdSelectBM_Click() ' moves the Selection to the current bookmark (if it exists) ' in the combobox drop down list ' selects it, unloads form If BookmarkExists(cboBookmarks.Text) Then ActiveDocument.Bookmarks(cboBookmarks.Text).Select Unload Me Else MsgBox "Bookmark doesn't exists" End If End Sub Private Sub cmdDeleteBM_Click() ' deletes the current selected bookmark (if it exists) ' in the combobox dropdown list (if not a formfield) ' clears the list, then repopulates it ' does NOT close form, so multiple actions ' are allowed Dim oBookmark As Word.Bookmark If BookmarkExists(cboBookmarks.Text) Then Set oBookmark = ActiveDocument.Bookmarks(cboBookmarks.Text) If ISFormfield(oBookmark.Name) = False Then oBookmark.Delete cboBookmarks.Clear FillBookmarkList Else MsgBox "Bookmark is a formfield" End If Else MsgBox "Bookmark doesn't exists" End If 'Clean up Set oBookmark = Nothing End Sub Private Sub cmdAddBookmark_Click() ' warns if you add a bookmark name that allready exists ' adds a bookmark at the current selection ' point, using a name entered by user ' clears the combobox dropdown list ' then repopulates it ' does NOT close form, so multiple selections ' added as bookmarks are allowed If txtNewBMName.Text = "" Then MsgBox "You must have a name for a new bookmark. " & _ "Please type a name in the New Bookmark Name field." txtNewBMName.SetFocus Exit Sub Else If BookmarkExists(txtNewBMName.Text) Then MsgBox "This bookmark allready exists" Else ActiveDocument.Bookmarks.Add Range:=Selection.Range, _ Name:=txtNewBMName.Text cboBookmarks.Clear FillBookmarkList txtNewBMName.Text = "" End If End If End Sub ' * * * * * * * * * * * * * * * * * * * ' Hiding and Showing Procedures ' * * * * * * * * * * * * * * * * * * * Private Sub cmdHide_Click() ' resize the form smaller, showing ' just the form title bar and label ' that fires the resize back to full size ' also moves it to the top of the screen ' this can be adjusted for various ' resolutions if needed With frmBookmarks .Height = 35 .Width = 130 .Top = 0 .Left = 300 End With ' display the label that resizes back ' to full size. Label Click and DblClick ' events fire the resize procedure MakeBigger lblShowMe.Visible = True End Sub Private Sub lblShowMe_Click() ' does nothing if the full form is visible ' otherwise, makes form full size If frmBookmarks.Height = 166 Then Exit Sub Else MakeBigger End If End Sub Private Sub lblShowMe_DblClick(ByVal Cancel As MSForms.ReturnBoolean) ' does nothing if the full form is visible ' otherwise, makes form full size If frmBookmarks.Height = 166 Then Exit Sub Else MakeBigger End If End Sub Sub MakeBigger() ' resizes form to full size ' positions it back to center screen With frmBookmarks .Height = 166 .Width = 225 .StartUpPosition = 1 .Show End With ' hide the label that fires MakeBigger lblShowMe.Visible = False End Sub Private Sub cmdDone_Click() ' closes the form Unload Me End Sub ' * * * * * * * * * * * * * * * * * * * ' Bookmark/Formfield support functions ' * * * * * * * * * * * * * * * * * * * Public Function BookmarkExists(sBookmark As String) As Boolean 'Checks if a bookmark exists in the active document If ActiveDocument.Bookmarks.Exists(sBookmark) Then BookmarkExists = True Else BookmarkExists = False End If End Function Public Function ISFormfield(sName As String) As Boolean Dim oFormField As Word.FormField 'Checks if bookmark IS a formfield If ActiveDocument.FormFields.Count = 0 Then ISFormfield = False Else For Each oFormField In ActiveDocument.FormFields() If oFormField.Name = sName Then ISFormfield = True End If Next End If End Function

How to use:

  1. Unzip the attachment.
  2. Open the document - Bookmarks.doc
  3. Press Alt-F11 to get the Visual Basic Editor, or use Tools > Macro > Visual Basic Editor
  4. If the Project Explorer is not visible, press Ctrl-R.
  5. If Project (Bookmarks) is not expanded in the Project Explorer click the plus sign beside "Project (Bookmarks)".
  6. Expand the Forms folder under project (Bookmarks).
  7. In the Project Explorer drag the item frmBookmarks to the project "Normal" and release.
  8. To make a keyboard macro to load the form, start recording a macro by Tools > Macro > Record New Macro. Give it a name - "LoadBookmarksForm". Make sure it is going to be saved in Normal.dot (this is the default). Click the keyboard icon on the macro dialog. Enter your keyboard shortcut then press OK.
  9. Immediately stop the macro. Do NOT record anything! Just stop it immediately.
  10. Go back into the Visual Basic Editor, expand Modules under Normal in the Project Explorer.
  11. Find your recorded macro. It will be blank, with no code.
  12. Between Sub and End Sub, type: "frmBookmarks.Show" - without the quotes.
 

Test the code:

  1. Open the form. Use your keyboard shortcut, or if you made a menu item use that.
  2. If there are no bookmarks in the active document the drop down list will show "no bookmarks".
  3. If there ARE bookmarks, all of them will be listed.
  4. Press the Go to Bookmark button to move the Selection point to the start of that bookmark. The bookmark is NOT selected.
  5. Press the Select Bookmark button to move the Selection to the bookmark and select it. The form closes.
  6. Press the Delete Bookmarks button to delete the currently selected bookmark in the list. The form does NOT close.
  7. Click in the document. The form is no longer active. Select some text. If needed, move the form around the screen to see the part you want to select.
  8. Once you have selected some text, click in the New Bookmark Name field on the form.
  9. Type the name you want to give the bookmark. You must have a name. The form will not create a bookmark without one.
  10. Press the Add as Bookmark button. This makes a bookmark of the text selected. It also updates the list of bookmark to include the new one. The form does NOT close. This is to allow making as many bookmarks as you want through a document, from one form. Normally Word does not allow this.
  11. Press the Hide button to shrink the form, and move it to the top of the screen. You may still move the shrunken form anywhere you like.
  12. If the shrunken form is at the top of the screen, one click will return it to full size. If it has been moved, use two clicks.
 

Sample File:

Bookmarks.zip 18.49KB 

Approved by mdmackillop


This entry has been viewed 241 times.

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