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