|
|
|
|
|
|
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
Private Sub UserForm_Initialize()
lblShowMe.Visible = False
FillBookmarkList
End Sub
Sub FillBookmarkList()
Dim mBookmark As Bookmark
On Error GoTo NoBookmarks
If ActiveDocument.Bookmarks.Count > 0 Then
For Each mBookmark In ActiveDocument.Bookmarks()
cboBookmarks.AddItem mBookmark.Name
Next
cboBookmarks.ListIndex = 0
Exit Sub
Else
GoTo NoBookmarks
End If
Exit Sub
NoBookmarks:
cboBookmarks.AddItem " no bookmarks "
cboBookmarks.ListIndex = 0
End Sub
Private Sub cmdGoToBookmark_Click()
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()
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()
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
Set oBookmark = Nothing
End Sub
Private Sub cmdAddBookmark_Click()
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
Private Sub cmdHide_Click()
With frmBookmarks
.Height = 35
.Width = 130
.Top = 0
.Left = 300
End With
lblShowMe.Visible = True
End Sub
Private Sub lblShowMe_Click()
If frmBookmarks.Height = 166 Then
Exit Sub
Else
MakeBigger
End If
End Sub
Private Sub lblShowMe_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If frmBookmarks.Height = 166 Then
Exit Sub
Else
MakeBigger
End If
End Sub
Sub MakeBigger()
With frmBookmarks
.Height = 166
.Width = 225
.StartUpPosition = 1
.Show
End With
lblShowMe.Visible = False
End Sub
Private Sub cmdDone_Click()
Unload Me
End Sub
Public Function BookmarkExists(sBookmark As String) As Boolean
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
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:
|
- Unzip the attachment.
- Open the document - Bookmarks.doc
- Press Alt-F11 to get the Visual Basic Editor, or use Tools > Macro > Visual Basic Editor
- If the Project Explorer is not visible, press Ctrl-R.
- If Project (Bookmarks) is not expanded in the Project Explorer click the plus sign beside "Project (Bookmarks)".
- Expand the Forms folder under project (Bookmarks).
- In the Project Explorer drag the item frmBookmarks to the project "Normal" and release.
- 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.
- Immediately stop the macro. Do NOT record anything! Just stop it immediately.
- Go back into the Visual Basic Editor, expand Modules under Normal in the Project Explorer.
- Find your recorded macro. It will be blank, with no code.
- Between Sub and End Sub, type: "frmBookmarks.Show" - without the quotes.
|
Test the code:
|
- Open the form. Use your keyboard shortcut, or if you made a menu item use that.
- If there are no bookmarks in the active document the drop down list will show "no bookmarks".
- If there ARE bookmarks, all of them will be listed.
- Press the Go to Bookmark button to move the Selection point to the start of that bookmark. The bookmark is NOT selected.
- Press the Select Bookmark button to move the Selection to the bookmark and select it. The form closes.
- Press the Delete Bookmarks button to delete the currently selected bookmark in the list. The form does NOT close.
- 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.
- Once you have selected some text, click in the New Bookmark Name field on the form.
- Type the name you want to give the bookmark. You must have a name. The form will not create a bookmark without one.
- 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.
- 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.
- 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.
|
|