Automatically Adding Hyperlinks in the Body and Footnotes of a MS Word Document
In the past I have posted about using vba code to automatically add hyperlinks into a Word document. The Tip of the Night for May 25, 2015 discussed how to add urls with a Visual Basic macro (to files saved on a network drive or a web address) after marking citations in the document in XE codes using the auto mark function. This code did not however work for citation references made in footnotes. The Tip of the Night for May 26, 2015 discussed a workaround using an add-in for MS Word called NoteStripper which converts footnotes to endnotes. The vba code works on the endnotes, which NoteStripper could then convert back to footnotes. However, formatting irregularities in the footnotes can prevent NoteStripper from reconverting the notes, and even when the reconversion worked, it was still necessary to carefully confirm that they were all placed back in the same spots in the document. However, the code works well in the body of a Word document, and in 2017 I posted a YouTube video describing the process.
I am now able to pass along an updated version of the vba code which will add links to both the body of a Word document and its footnotes at the same time without the need to bother with the often cumbersome process of converting the footnotes to endnotes, and reconverting them back again. Oscar Sun posted a response to my inquiry for a solution on stackoverflow.
The below visual basic code works the same way as the code discussed back in May 2015, converting urls added to XE codes to hyperlinks - but this version processes both the body and the footnotes at the same time. The steps can be followed in the below animated gif.
The key to editing the vba code is to enter the full path to the folder containing the documents you are linking to with only the first letter of that folder on the line of code beginning: If Left$(url, 4) = "
. . . change the 4 to the character count for that truncated path. So for example:
If Left$(url, 14) = "C:\foofolder\e" Then
. . . because 'C:\foofolder\e' is 14 characters long.
On the next line you want to account for how many words are used in the citations you are linking to. In this case since each citation is two words, 'Exhibit 01'; 'Exhibit 02', etc., we enter:
isHyper = 2
That's it. So long as you can generate a list of the citations in your brief or expert report and put them in a table with the corresponding PDFs you're linking to, you can automatically add 10 or 10,000 hyperlinks in your Word document.
Sub MakeHyperlinks() Dim afield As Field Dim url As String Dim isHyper As Integer Dim sr As Range For Each sr In ActiveDocument.StoryRanges 'For Each afield In ActiveDocument.Fields For Each afield In sr.Fields If afield.Type = wdFieldIndexEntry Then isHyper = 0 afield.Select Selection.Collapse url = Right$(afield.Code, Len(afield.Code) - 5) url = Left$(url, Len(url) - 2) If Left$(url, 4) = "../F" Then isHyper = 1 End If If Left$(url, 4) = "../T" Then isHyper = 2 End If If isHyper <> 0 Then Selection.MoveStart unit:=wdCharacter, Count:=-3 Selection.MoveStart unit:=wdWord, Count:=-isHyper afield.Delete ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _ Address:=url End If End If Next afield Next sr End Sub
Comments