Hi. I am new with VBA. I am trying to create VBA code that automatically generates a bookmark. The name of the bookmark will be the selected text (with spaces removed). The code I have created looks like this:

Sub AutoBookmark()

Dim noSpace As String
Selection.Copy
noSpace = Replace(Selection.Text, " ", "")

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=noSpace
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub


I get an error when running the code saying "bad bookmark name". Does anyone have any suggestions?

Thanks