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):
It keeps the formating of the Index. Just don't forget to set nice styles for "hyperlink" and "visited hyperlink".
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".
This is an old discussion that has not been active in a long time. Before commenting here, you should strongly consider starting a new discussion instead. If you think the content of this discussion is still relevant, you can link to it from your new discussion.
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!).