Graham Mayor

... helping to ease the lives of Microsoft Word users.

Many people access the material from this web site daily. Most just take what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved would help to ensure the continued availability of this resource. Click the appropriate button above to access PayPal.

Add hyperlinks to footnotes and endnotes

While if you manually type footnote or endnote texts web addresses and e-mail addresses in Word documents, they will be converted to hyperlinks using the autoformat as you type option, it is not possible to subsequently convert such addresses to hyperlinks using the autoformat process. Autoformat does not see the footnote/endnote ranges.

A user question in a forum prompted me to look for a solution, and with a little lateral thinking it occurred to me that it would be possible to temporarily write the footnotes and endnotes in turn to a new document, where they could easily be autoformatted, then write the formatted versions back to the footnote/endnote ranges.

In practice it works well, and I include two versions of the code below.

The first is a stand-alone macro intended to be used with the current document

If you are unsure what to do with macro listings see http://www.gmayor.com/installing_macro.htm

Option Explicit

Sub LinkNotes()
Dim oDoc As Document
Dim oTemp As Document
Dim oNote As Range
Dim oFN As Footnote
Dim oEN As EndNote
Dim oRng As Range
Set oDoc = ActiveDocument
oDoc.Save
Set oTemp = Documents.Add(Template:=oDoc.FullName, Visible:=False)
For Each oFN In oDoc.Footnotes
Set oNote = oFN.Range
Set oRng = oTemp.Range
oRng.FormattedText = oNote.FormattedText
Options.AutoFormatReplaceHyperlinks = True
oRng.AutoFormat
oRng.End = oRng.End - 1
oNote.FormattedText = oRng.FormattedText
Next oFN
For Each oEN In oDoc.Endnotes
Set oNote = oEN.Range
Set oRng = oTemp.Range
oRng.FormattedText = oNote.FormattedText
Options.AutoFormatReplaceHyperlinks = True
oRng.AutoFormat
oRng.End = oRng.End - 1
oNote.FormattedText = oRng.FormattedText
Next oEN
oTemp.Close savechanges:=wdDoNotSaveChanges
lbl_Exit:
Set oEN = Nothing
Set oFN = Nothing
Set oDoc = Nothing
Set oTemp = Nothing
Set oRng = Nothing
Set oNote = Nothing
Exit Sub
End Sub

The second code sequence is a function intended for use with my Document Batch Processing add-in as a custom process:

Option Explicit

Function LinkNotes(oDoc As Document) As Boolean
Dim oTemp As Document
Dim oNote As Range
Dim oFN As Footnote
Dim oEN As EndNote
Dim oRng As Range
On Error GoTo err_Handler
Set oTemp = Documents.Add(Template:=oDoc.FullName, Visible:=False)
For Each oFN In oDoc.Footnotes
Set oNote = oFN.Range
Set oRng = oTemp.Range
oRng.FormattedText = oNote.FormattedText
Options.AutoFormatReplaceHyperlinks = True
oRng.AutoFormat
oRng.End = oRng.End - 1
oNote.FormattedText = oRng.FormattedText
Next oFN
For Each oEN In oDoc.Endnotes
Set oNote = oEN.Range
Set oRng = oTemp.Range
oRng.FormattedText = oNote.FormattedText
Options.AutoFormatReplaceHyperlinks = True
oRng.AutoFormat
oRng.End = oRng.End - 1
oNote.FormattedText = oRng.FormattedText
Next oEN
oTemp.Close savechanges:=wdDoNotSaveChanges
LinkNotes = True
lbl_Exit:
Set oEN = Nothing
Set oFN = Nothing
Set oTemp = Nothing
Set oRng = Nothing
Set oNote = Nothing
Exit Function
err_Handler:
LinkNotes = False
Resume lbl_Exit
End Function

Both code sequences work in the same way. Each footnote and/or each endnote is copied to a new temporary document where it is auto-formatted. The temporary document is opened invisibly so the user is unaware of its presence. The formatted footnote and endnote ranges are copied in turn to the temporary document, where they are auto-formatted to add the hyperlinks, before being returned formatted to their original locations. Finally the temporary document is closed without saving any changes.

Where the batch process is used a new temporary document is created for each processed document.

 

 

Batch Process

The code featured in the latter half of this page can be used in conjunction with the Document Batch Processes add-in.