Microsoft Office Tutorials and References
In Depth Information
Creating a Bookmark from a Selection
If doc.Bookmarks.Exists(BookmarkName) Then¶
'Calculate incremented name¶
BookmarkName = Left(BookmarkName, _¶
Len(BookmarkName) - Len(var.Value)) & var.Value¶
var.Value = CStr(CLng(var.Value) + 1)¶
End If¶
CheckIfDuplicateName = BookmarkName¶
End Function¶
' * * * * *¶
Function varExists(doc As Word.Document, _¶
s As String) As Boolean¶
'Variable declaration¶
Dim var As Word.Variable¶
varExists = False¶
'Loop through the list of document variables¶
'and check whether it already exists by¶
'comparing the name¶
For Each var In doc.Variables¶
If var.Name = s Then¶
varExists = True¶
Exit For¶
End If¶
Next var¶
End Function¶
Follow these steps:
1. Copy the entire set of macros to a module in the document, in its
template, in, or in any template that will be loaded as a
global Add-in. Then assign it to a toolbar button and/or keyboard
shortcut. See Running a Macro from a Toolbar Button on page 418 or
Running a Macro Using Shortcut Keys on page 419 for help in assigning
a macro to a toolbar button or a keyboard shortcut.
2. The incremental numbers for duplicate names and bookmarks without
content are stored in document Variables. The names for the Variables
are set as Const values at the beginning of the module. To use different
names, change the values in quotation marks.
3. To type in a bookmark name when no text is selected to provide the
bookmark name, remove the apostrophes from the lines of code below,
and comment out the original code:
If Selection.Type = wdSelectionIP Then¶
'The user didn't select any text; a bookmark without¶
'content will be inserted with¶
'an incremented name txt#¶
'Calculate that name¶
BookmarkName = "txt" & var.Value¶
var.Value = CStr(CLng(var.Value) + 1)¶
'Alternately, a prompt can be displayed¶
Search JabSto ::

Custom Search