Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Bookmarks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Francis Hookham - 16 Jun 2007 17:55 GMT
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
Jay Freedman - 16 Jun 2007 20:31 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.