Microsoft Office Tutorials and References
In Depth Information
Setting Hyperlinks on Index Entries
' * * * * *¶
Sub CreateHyperlinkAndTarget(doc As Word.Document, _¶
searchTerm As String, bookmarkName As String, _¶
linkCounter As Long, rngIndex As Word.Range)¶
'Variable declaration¶
Dim rngPage As Word.Range¶
Dim rngAnchor As Word.Range¶
'In order to assign a page to a range the¶
'selection must be on that page. So be sure¶
'the document being processed is the active one¶
If Not doc Is ActiveDocument Then doc.Activate¶
Selection.GoTo What:=wdGoToPage, _¶
Count:=CLng(EntryList(linkCounter).page)¶
Set rngPage = doc.Bookmarks("\Page").Range¶
'Now search for the term on the given page¶
'Be sure to also check the XE fields, even if¶
'they are not displayed¶
rngPage.TextRetrievalMode.IncludeHiddenText = True¶
With rngPage.Find¶
.ClearFormatting¶
.Text = searchTerm¶
.Forward = True¶
If .Execute Then¶
'If found, bookmark it¶
bookmarkName = bookmarkName & CStr(nrEntries)¶
doc.Bookmarks.Add Name:=bookmarkName, Range:=rngPage¶
'get the range of the page number and¶
Set rngAnchor = rngIndex.Duplicate¶
rngAnchor.TextRetrievalMode.IncludeFieldCodes = False¶
rngAnchor.SetRange _¶
Start:=EntryList(linkCounter).posStart, _¶
End:=EntryList(linkCounter).posEnd¶
'Insert hyperlink to the bookmark in its place¶
'With the page number as the display text¶
doc.Hyperlinks.Add Anchor:=rngAnchor, _¶
SubAddress:=bookmarkName, _¶
TextToDisplay:=EntryList(linkCounter).page¶
'For Word97, remove the TextToDisplay part¶
nrEntries = nrEntries + 1¶
End If¶
End With¶
End Sub¶
Wrd
Tip: Be sure to update the index before running the macro, because the macro turns the index
into static text. It does not matter whether or not the XE (index entry) fields are visible on
screen.
If there is a subsequent need to update the index, delete the hyperlinked index, insert a
new one, and then run the macro again.
Search JabSto ::




Custom Search