Bookmarks

F

Francis Hookham

The sub below works well being run from a button on a toolbar.

It would be nice to cut out the InputBox and instead select the text and let
the macro copy that, remove white space and use that as the BookmarkName.

I have tried a couple of times but am missing something - brains probably!



Thanks



Francis Hookham



Sub SetBookmarks()

BookmarkName = InputBox("Bookmark name?")

With ActiveDocument.Bookmarks

.Add Range:=Selection.Range, Name:=BookmarkName

.DefaultSorting = wdSortByName

.ShowHidden = False

End With

Windows("Club Officers 07-08.doc").Activate

End Sub
 
J

Jay Freedman

I'm not quite sure whether your "remove white space" refers to white
space at the beginning and end of the selection, or to white space
within the selection (i.e., selection is multiple words). I'll assume
you want both.

You also need some error-handling in case there are other characters
in the selection that aren't valid in bookmark names.

Sub SetBookmarks()
Dim BookmarkName As String
' remove white space at ends
BookmarkName = Trim(Selection.Text)
' remove blanks and tabs in middle
BookmarkName = Replace(BookmarkName, " ", "")
BookmarkName = Replace(BookmarkName, vbTab, "")
BookmarkName = Replace(BookmarkName, vbCr, "")

If Len(BookmarkName) = 0 Then
MsgBox "Please select the bookmark name"
Exit Sub
End If

If Len(BookmarkName) > 40 Then
MsgBox "The bookmark name " & BookmarkName & _
" is too long"
Exit Sub
End If

On Error GoTo ErrHdl
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=BookmarkName
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
On Error GoTo 0

Windows("Club Officers 07-08.doc").Activate

Exit Sub
ErrHdl:
MsgBox BookmarkName & " is not a valid bookmark name"
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top