B
bmurphy
I wrote the macro below (Word 2002) to put hyperlinks on page numbers
in a Word generated index. It works great on a file of mine which is
over 270 pages.
It's not real fancy. It finds the index field, selects it, updates
it, cuts it, pastes it as plain text, runs through the page numbers in
the index, moves to the page, puts a bookmark at the top of the page,
and hyperlinks the page number back in the index to the bookmark.
In my particular case, all of my index fields {xe "blah blah"} happen
to be at the tops of pages. So this solution works fine for me.
Otherwise, I guess I would jump to the start of the page, and from
there search for {xe "blah blah"} and put the bookmark there.
A PDF I make from the file works like it should. Which was the reason
for doing this. If the document is edited, the index has to be
deleted and regenerated.
I hope someone finds it useful. If so, let me know.
Cheers,
Brian Murphy
Austin, Texas
Sub Hyperlink_Index()
Dim s$, s1$, n1&, n2&, nEnd&
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = Not
ActiveWindow.View.ShowFieldCodes
Selection.Find.ClearFormatting
With Selection.Find
.Text = "index \c"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ActiveWindow.View.ShowFieldCodes = Not
ActiveWindow.View.ShowFieldCodes
Selection.Fields.Update
n1 = Selection.Start
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
Selection.PasteAndFormat (wdFormatPlainText)
nEnd = Selection.End
ActiveDocument.Range(n1, n1).Select
Do
ActiveDocument.Range(Selection.End, Selection.End).Select:
Selection.MoveRight Unit:=wdWord, Extend:=wdWord
s = Selection.Text
If s = ", " Then
ActiveDocument.Range(Selection.End, Selection.End).Select:
Selection.MoveRight Unit:=wdWord, Extend:=wdWord
s = Selection.Text
If IsNumeric(s) Then
n1 = Selection.Start
n2 = Selection.End
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext,
Name:=s
ActiveDocument.Range(Selection.End,
Selection.End).Select: Selection.MoveRight Unit:=wdWord,
Extend:=wdWord
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="pg_" & s
End With
ActiveDocument.Range(n1, n2).Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:="", _
SubAddress:="pg_" & s, ScreenTip:=""
End If
End If
Loop Until Selection.End >=
ActiveDocument.Bookmarks("\EndOfDoc").Start - 1
End Sub
in a Word generated index. It works great on a file of mine which is
over 270 pages.
It's not real fancy. It finds the index field, selects it, updates
it, cuts it, pastes it as plain text, runs through the page numbers in
the index, moves to the page, puts a bookmark at the top of the page,
and hyperlinks the page number back in the index to the bookmark.
In my particular case, all of my index fields {xe "blah blah"} happen
to be at the tops of pages. So this solution works fine for me.
Otherwise, I guess I would jump to the start of the page, and from
there search for {xe "blah blah"} and put the bookmark there.
A PDF I make from the file works like it should. Which was the reason
for doing this. If the document is edited, the index has to be
deleted and regenerated.
I hope someone finds it useful. If so, let me know.
Cheers,
Brian Murphy
Austin, Texas
Sub Hyperlink_Index()
Dim s$, s1$, n1&, n2&, nEnd&
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = Not
ActiveWindow.View.ShowFieldCodes
Selection.Find.ClearFormatting
With Selection.Find
.Text = "index \c"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ActiveWindow.View.ShowFieldCodes = Not
ActiveWindow.View.ShowFieldCodes
Selection.Fields.Update
n1 = Selection.Start
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
Selection.PasteAndFormat (wdFormatPlainText)
nEnd = Selection.End
ActiveDocument.Range(n1, n1).Select
Do
ActiveDocument.Range(Selection.End, Selection.End).Select:
Selection.MoveRight Unit:=wdWord, Extend:=wdWord
s = Selection.Text
If s = ", " Then
ActiveDocument.Range(Selection.End, Selection.End).Select:
Selection.MoveRight Unit:=wdWord, Extend:=wdWord
s = Selection.Text
If IsNumeric(s) Then
n1 = Selection.Start
n2 = Selection.End
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext,
Name:=s
ActiveDocument.Range(Selection.End,
Selection.End).Select: Selection.MoveRight Unit:=wdWord,
Extend:=wdWord
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="pg_" & s
End With
ActiveDocument.Range(n1, n2).Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:="", _
SubAddress:="pg_" & s, ScreenTip:=""
End If
End If
Loop Until Selection.End >=
ActiveDocument.Bookmarks("\EndOfDoc").Start - 1
End Sub