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
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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
>The sub below works well being run from a button on a toolbar.
>
[quoted text clipped - 24 lines]
>
>End Sub