top of page

How to Automate the Insertion of Hyperlinks in MS Word


I came across this very useful description of how to automatically insert hyperlinks to cited exhibits or bates numbers in a Word document at http://word.mvps.org/faqs/general/AutoHyperlinks.htm , When an attorney prepares an outline for a deposition or a cross examination she or he will often cite to dozens of Bates numbers or exhibits numbers. The outline can be made a lot more useful if each cited document is hyperlinked to. Follow this method which is also shown in the screen grabs below.

1. Start with a Word document that cites to Bates numbers or exhibit numbers which are also used as the file names of PDFs or other files.

2. Create a separate Word document with a table with two columns. The first column should contain the bates number or exhibit number as cited in the outline. The second column should contain the full path of the associated file. This is a Concordance table. Save it on your local drive in a place where you can reach it later.

3. Insert the below macro in Visual Basic for the document you're working on. You need to edit it so that it points to the folder where you have the files saved. Where it says, "Left$(url, 4) = "../F"" enter in the path of the folder to replace '../F' but only include the first letter of the last subfolder which actually has the PDFs or other files. Then get the character count (with spaces) of this truncated version of the file path, and replace 'url, 4' with 'url, X'.

Sub MakeHyperlinks() Dim afield As Field Dim url As String Dim isHyper As Integer

For Each afield In ActiveDocument.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

End Sub

4. In order to get the character count in Word 2010 go to Review . . .Word Count.

5. Next go back to the outline, click on the Reference tab, and select Insert Index.

6. Select AutoMark, and then browse to and select the Word document with the concordance table you created in step 2.

7. The links will be inserted the XE fields in Word.

8. Go to View ... Macros and select 'MakeHyperlinks', and then click Run.

9. The hyperlinks will be automatically added!

Note if there are Bates numbers containing hyphens, you may wish to find and replace these in Word before you begin. If you don't, the generated hyperlinks will not cover the full length of the cited bates numbers. After the macro is run, you can re-insert the hyphens, and the links will still work.


bottom of page