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 / February 2006

Tip: Looking for answers? Try searching our database.

Error automatically adding bookmarks in Word 2003

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarkB - 14 Feb 2006 16:32 GMT
Hi, i am having trouble creating a macro that creates a bookmark, then uses
the selection as the name.
I have written the following macro...

Sub AddBookmarkUsingSelection()
   Dim strSelection40 As String
   Dim lngBookmarkCount As Long
   strSelection40 = Left(Replace(Selection, " ", ""), 40)
   lngBookmarkCount = Application.ActiveDocument.Bookmarks.Count
   With ActiveDocument.Bookmarks
       .Add strSelection40, Selection.Range
       .DefaultSorting = wdSortByLocation
       .ShowHidden = False
   End With
   MsgBox "Bookmark number " & lngBookmarkCount + 1 & " added: " &
strSelection40, vbInformation
End Sub

... but as soon as i run it, it gets to the '.Add strSelection40,
Selection.Range' range then stopps with an error: 5828 - Bad bookmark name.
If i analyse the contents of the variable strSelection40 in the Immediate
window it confirms that the string contains no blanks. If i then assign the
same string value back to the string, the macro runs as required, see
Immediate below:

? strSelection40
Mainnut
strSelection40 = "Mainnut"

Does anyone know a workarround for this?
Thanks in advance.
Mark
Dave Lett - 14 Feb 2006 16:51 GMT
Hi Mark,

Bookmark names cannot have a whole range of other characters. The following
might eliminate most of those problems:

Sub AddBookmarkUsingSelection()
   Dim strSelection40 As String
   Dim lngBookmarkCount As Long
   strSelection40 = fGoodBookMarkName(sName:=Left(Replace(Selection, " ",
""), 40))
   lngBookmarkCount = Application.ActiveDocument.Bookmarks.Count
   With ActiveDocument.Bookmarks
       .Add strSelection40, Selection.Range
       .DefaultSorting = wdSortByLocation
       .ShowHidden = False
   End With
   MsgBox "Bookmark number " & lngBookmarkCount + 1 & " added: " & _
       strSelection40, vbInformation
End Sub
Public Function fGoodBookMarkName(sName As String) As String
fGoodBookMarkName = sName
Dim lCount As Long
For lCount = 0 To 255
   Select Case lCount
       Case 48 To 57, 65 To 90, 97 To 122
           ''' these are valid characters
       Case Else
           fGoodBookMarkName = Replace(fGoodBookMarkName, Chr(lCount), "")
   End Select
Next lCount
End Function

HTH,
Dave

> Hi, i am having trouble creating a macro that creates a bookmark, then uses
> the selection as the name.
[quoted text clipped - 28 lines]
> Thanks in advance.
> Mark
Greg - 14 Feb 2006 18:05 GMT
Dave,

You should call that Function fNotAlwaysGoodBookMarkName.

I like your method, but you seem to have forgotten that a bookmark name
must start with the range A-z ;-)
Greg - 14 Feb 2006 17:57 GMT
Mark,

Your code ran fine here with the example mainnut selected.

I will add that there is alot more than " " that will cause an invalid
bookmark name.  Consider:

Sub AddBookmarkUsingSelection()
Dim oStr As String
Dim i As Long
oStr = CheckNameString(Selection.Range)
i = Application.ActiveDocument.Bookmarks.Count
With ActiveDocument.Bookmarks
 .Add oStr, Selection.Range
 .DefaultSorting = wdSortByLocation
 .ShowHidden = False
End With
MsgBox "Bookmark number " & i + 1 _
& " added: " & oStr, vbInformation
End Sub

Function CheckNameString(strIn As String)
Dim oFirstChr As String
Dim i As Long
Dim tempStr As String
strIn = Left(Trim(strIn), 40)
oFirstChr = Right(strIn, 1)
If oFirstChr = "" Then Exit Function
If Not oFirstChr Like "[A-z]" Then
   strIn = "A_" & strIn
End If
For i = 1 To Len(strIn)
If InStr(" `!@#$%^&*()-+=\|]}[{,<.>/?;:""'", _
  Mid$(strIn, i, 1)) > 0 Then
 tempStr = tempStr & "_"
Else
 tempStr = tempStr & Mid$(strIn, i, 1)
End If
Next i
CheckNameString = tempStr
End Function
Greg - 14 Feb 2006 18:19 GMT
Mark and Dave,

Seems like I have confused my right and left.

I liked Dave's Select Case method.  I incorporated into my earlier
proposal (also fixed so that we check the first character on the left
(vice the right) of the string for the range A-z)

Hope this helps:

Option Explicit
Sub AddBookmarkUsingSelection()
Dim oStr As String
Dim i As Long
oStr = CheckNameString(Selection.Range)
i = Application.ActiveDocument.Bookmarks.Count
With ActiveDocument.Bookmarks
 .Add oStr, Selection.Range
 .DefaultSorting = wdSortByLocation
 .ShowHidden = False
End With
MsgBox "Bookmark number " & i + 1 _
& " added: " & oStr, vbInformation
End Sub
Function CheckNameString(strIn As String)
Dim oFirstChr As String
Dim i As Long
Dim tempStr As String
strIn = Left(Trim(strIn), 40)
oFirstChr = Left(strIn, 1)
If oFirstChr = "" Then Exit Function
If Not oFirstChr Like "[A-z]" Then
   strIn = "A_" & strIn
End If
For i = 1 To Len(strIn)
 Select Case Asc(Mid$(strIn, i, 1))
   Case 65 To 90, 97 To 122, 95
     tempStr = tempStr & Mid$(strIn, i, 1)
   Case Else
     tempStr = tempStr & "_"
 End Select
Next i
'For i = 1 To Len(strIn)
'If InStr(" `!@#$%^&*()-+=\|]}[{,<.>/?;:""'", _
'   Mid$(strIn, i, 1)) > 0 Then
'  tempStr = tempStr & "_"
'Else
'  tempStr = tempStr & Mid$(strIn, i, 1)
'End If
'Next i
CheckNameString = tempStr
End Function
Greg Maxey - 14 Feb 2006 23:06 GMT
Mark, Dave

I forgot about numbers.

Change the Case statement to:
Case 48 To 57, 65 To 90, 97 To 122, 95

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Mark and Dave,
>
[quoted text clipped - 48 lines]
> CheckNameString = tempStr
> End Function
MarkB - 15 Feb 2006 08:17 GMT
Firstly I want to say thank you much for your excellent responses. I'm an
established Excel and Access vb programmer and I'm getting my teeth into
vb.net and really enjoying it. I have not had much chance to program Word and
Outlook vb.

Secondly, I realised that other characters except the space character could
cause a problem – the particular case I used in the example was a heading
called "Main nut" in an Engineering reference manual that I'm writing (I'm a
Engineer by trade). But your functions lead me to a clue as to why my
original sub-procedure failed: I was also selecting (and therefore
transmitting) the trailing edge return character, as can be seen when I use
the functions provided here. The resultant bookmark is called 'Main_nut_'!

I have simply inserted a line to ensure that the final underscore is not
written. Once again thank you very much, the responses here have been a great
help.

Mark
 
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.