Error automatically adding bookmarks in Word 2003

M

MarkB

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
 
D

Dave Lett

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
 
G

Greg

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
 
G

Greg

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 ;-)
 
G

Greg

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
 
G

Greg Maxey

Mark, Dave

I forgot about numbers.

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

MarkB

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
 

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