Word macro for hyperlinking Index page numbers

i just messed around with visual basic a little. Maybe someone may find this useful (it is not directly realted to zotero, but it is related to writing and i don't know where to publish it otherwise):

Sub Hyperlink_Index()
' based on
' http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2008-02/msg00230.html
' by
' Brian Murphy
' Austin , Texas
Dim s$, s1$, n1&, n2&, nEnd&
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = True
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 = False
ActiveWindow.ActivePane.View.ShowAll = False
ActiveWindow.View.ShowPicturePlaceHolders = False
Selection.Fields.Update
n1 = Selection.Start
'Selection.EndKey Unit:=wdStory, Extend:=wdExtend
'Selection.Cut
'Selection.PasteAndFormat (wdFormatPlainText)
'Selection.Paste
nEnd = Selection.End
'ActiveDocument.Range(n1, n1).Select

ActiveDocument.Range(Selection.Start, Selection.Start).Select
Selection.MoveRight Unit:=wdCharacter, Count:=1

Do
ActiveDocument.Range(Selection.End, Selection.End).Select:
Selection.MoveRight Unit:=wdWord, Extend:=wdWord
s = Selection.Text
If s = ", " Or Right(s, 2) = " " 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
ActiveWindow.View.ShowPicturePlaceHolders = False
End Sub


It keeps the formating of the Index. Just don't forget to set nice styles for "hyperlink" and "visited hyperlink".
  • If you refresh the index, the hyperlinks will be lost. You can use the macro for refreshing though.

    PS: One could still improve it with a function to search for the *real* index on the page, so that it is linked to the index not only to the page (if someone implements this it should be optional though!).
  • I tried this and it starts at the top of the document and attempts to hyperlink all numbers that appear after a comma. The problem is my index is at the end of the document and the top of the document has a cover page with a date (that has a comma and the year). Then it runs through the document until it encounters an error on page 3, well before it ever got to the index on page 273.
Sign In or Register to comment.