Microsoft Office Tutorials and References
In Depth Information
Creating a Bookmark from a Selection
BookmarkName = ProcessBookmarkName(rng.Text)¶
End If¶
'Check if the bookmark name already exists;¶
'if it does, it is incremented with a counter¶
BookmarkName = "txt" & CheckIfDuplicateName( _¶
ActiveDocument, BookmarkName)¶
'Insert the bookmark¶
ActiveDocument.Bookmarks.Add _¶
Name:=BookmarkName, Range:=rng¶
End Sub¶
' * * * * *¶
Function ProcessBookmarkName(s As String) As String¶
'Variable declaration¶
Dim i As Long¶
'Maximum length of a bookmark name is 40 characters¶
'Because txt will be added to the beginning¶
'therefore cut off at 37¶
If Len(s) > 37 Then s = Left(s, 37)¶
'Replace all spaces with underline characters¶
s = Replace(s, " ", "_")¶
'Remove any numbers at the beginning¶
Do While IsNumeric(Left(s, 1)) = True¶
s = Mid(s, 2)¶
Debug.Print s¶
Loop¶
'Remove invalid characters¶
'(following list is not comprehensive)¶
For i = 1 To Len(s)¶
Select Case Mid(s, i, 1)¶
Case "§", "°", "+", "¦", "@", Chr$(34), "*", _¶
"#", "%", "&", "", "/", "|", "(", "¢", ")", _¶
"=", "?", "´", "^", "`", "~", "[", "]", _¶
"¨", "!", "{", "}", "$", "£", "<", ">", "<", _¶
".", ",", ":", ";", "-"¶
s = Left(s, i - 1) & Mid(s, i + 1)¶
Case Else¶
'Otherwise, do nothing¶
End Select¶
Next i¶
ProcessBookmarkName = s¶
End Function¶
' * * * * *¶
Function CheckIfDuplicateName(doc As Word.Document, _¶
BookmarkName As String) As String¶
'Variable declaration¶
Dim var As Word.Variable¶
If varExists(doc, varDuplicateName) = False Then¶
ActiveDocument.Variables.Add _¶
Name:=varDuplicateName, Value:="1"¶
End If¶
Set var = ActiveDocument.Variables(varDuplicateName)¶
Wrd
Search JabSto ::




Custom Search