PDA

View Full Version : applying hyperlink styles in footnotes and endnotes



leprendun
05-01-2015, 06:27 AM
Hello,
We have a large number of documents that contain URLs in the footnotes and endnotes. We would like to convert them all to actual clickable hyperlinks, formatted with the hyperlink style. This is absurdly easy to do in the main text with the AutoFormat method, but apparently that method can't be used in footnotes and endnotes! The URLs all begin with the standard h t t p (without the spaces, thanks spam guards) but they are not formatted in any other consistent manner. The URLs are surrounded by other text, so it's not just hyperlinks within each note.

I found a potential starting point in the "Solved: Make all URLs if footnotes clickable?" thread (can't post a link), but the implementation is fairly complicated, requires that footnotes all be converted to endnotes, and documents a case where URLs are all formatted with brackets, making them easier to identify.

Any suggestions?

gmayor
05-02-2015, 02:09 AM
You can't use autoformat in the notes themselves, but you can copy the notes to a document, format them there then copy them back again. The following should work



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
oRng.Style = "Footnote Text"
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
oRng.Style = "Footnote Text"
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


With a few small modifications to that code you should also be able to use it with http://www.gmayor.com/document_batch_processes.htm as a custom process to enable you to batch process your documents.


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
oRng.Style = "Footnote Text"
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
oRng.Style = "Footnote Text"
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 oDoc = Nothing
Set oTemp = Nothing
Set oRng = Nothing
Set oNote = Nothing
Exit Function
err_Handler:
LinkNotes = False
Resume lbl_Exit
End Function

gmayor
05-03-2015, 03:55 AM
On further reflection, if you are planning to use the second suggestion with the batch process, don't set oDoc to Nothing in the cleanup section at the end of the function.

I have prepared a web page that covers the process and includes some modifications to the code

http://www.gmayor.com/add_hyperlinks_to_footnotes.htm

leprendun
05-04-2015, 10:07 AM
Thank you for your thorough replies! I'll try implementing this soon and let you know how it goes. It is such a complicated solution to what I would have thought was a simple fix. I also thought of using a regular expression to scour the entire document but couldn't get the patterns to match (I'll post on that issue when I get a moment).

leprendun
05-04-2015, 10:43 AM
OK, that was much easier to implement than I anticipated and it worked beautifully. We use a number of custom styles in endnotes and footnotes and they were all preserved throughout the process. Thank you so much for your help.