Hyperlinks from Headings to the TOC

by Allen Wyatt
(last updated April 12, 2014)

8

Peter writes manuals and can autogenerate a chapter's Table of Contents in the normal way. This allows readers to click on a TOC entry and jump to the heading, but there is no way to click on the heading and get back to the TOC. So readers can do this Peter has to individually apply a hyperlink to the heading and then get rid of the underlining, which he finds unsightly. A manual's chapter can contain hundreds of headings, so the task of applying the hyperlinks individually can be painful. Peter wonders if there is a way to automate the adding of hyperlinks to headings as he needs.

Peter's approach, while well intentioned, is completely unnecessary. Word does provide a way to click a heading in the TOC (which Peter knows) and then return to that heading in the TOC (which Peter apparently doesn't know). The specifc shortcut for accomplishing this task is Alt+Left Arrow. The shortcut jums back to where you were before clicking on the hyperlink in the TOC, which makes it great for long tables of contents—you are returned to exactly where you were rather than to the beginning of the TOC.

The shortcut key is the best solution for readers; it works by default on a standard Word installation. If wanted, however, you could add a Back button to your Quick Access Toolbar that could be clicked to return to the TOC's point of departure. Follow these steps:

  1. Display the Word Options dialog box. (In Word 2007 click the Office button and then click Word Options. In Word 2010 and Word 2013 display the File tab of the ribbon and then click Options.)
  2. At the left side of the dialog box click Customize (Word 2007) or Quick Access Toolbar (later versions of Word). (See Figure 1.)
  3. Figure 1. The Quick Access Toolbar area of the Word Options dialog box.

  4. Using the Choose Commands From drop-down list, choose All Commands.
  5. In the list of commands, locate and select the Back command.
  6. Click the Add button. The Back command now appears at the right side of the dialog box, in the list of Quick Access Toolbar commands.
  7. Use the Up and Down arrow buttons to situate the Back command within the Quick Access Toolbar commands.
  8. Click OK.

The Back command, which now appears on the Quick Access Toolbar, performs the exact same function as the Alt+Left Arrow shortcut.

If you still want to add hyperlinks to headings, the only way to automate the process is to use a macro. The following macro steps through each entry in the first TOC in the document, finds the referenced heading in the main body of the text, and then adds a hyperlink back to the TOC.

Sub HyperlinkHeadings()
    Dim hyp As Hyperlink
    Dim toc As TableOfContents
    Dim k As Long
    Dim bkmk As String
    Dim sCode As String
    Dim fld As Field
    Dim aRange As Range

    If ActiveDocument.TablesOfContents.Count = 0 Then
        MsgBox "There are no Tables of Contents in document"
        Exit Sub
    End If

    Set toc = ActiveDocument.TablesOfContents(1)

    For Each fld In toc.Range.Fields
        sCode = fld.Code.Text
        If InStr(sCode, "HYPERLINK") > 0 Then
            bkmk = Mid(sCode, InStr(sCode, "_"))
            bkmk = Left(bkmk, Len(bkmk) - 2)
            fld.Select
            ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
              Name:=bkmk & "R"

            Set aRange = ActiveDocument.Bookmarks(bkmk).Range
            aRange.Select
            With ActiveDocument.Hyperlinks.Add(Anchor:=Selection.Range, _
                Address:="", SubAddress:=bkmk & "R", _
                  TextToDisplay:=Selection.Text)
                .Range.Select
                Selection.ClearCharacterAllFormatting
            End With
        End If
    Next fld

    Options.CtrlClickHyperlinkToOpen = False
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13059) applies to Microsoft Word 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off HTML Conversions

Don't want Word to load up your HTML documents as formatted text? There are a couple of ways you can instruct Word to be more ...

Discover More

Making a Cell's Contents Italics within a Macro

You can use macros to process information in your worksheets. You may want to use that macro to apply the italic attribute to ...

Discover More

Using Find and Replace to Change Text Case

Can you really use Find and Replace to change the case of text in your document? Not really, but that shouldn't stop you from ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Using Dot Leaders in Special Tables

Adding dot leaders before page numbers in a table of contents or table of authorities can make the finished table look ...

Discover More

Creating a Table of Contents from Heading Levels

If your document is any length at all, adding a table of contents is a nice touch. This tip demonstrates how easy it is to ...

Discover More

Creating a Table of Contents from TOC Fields

If you inserted a bunch of TOC fields in your document, you can create your table of contents quite easily based on those ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 8?

2015-08-26 07:25:31

Ken Endacott

The error occurs if there is no table of contents in the document hence the test at the start of the macro for TableOfContents.Count = 0

Otherwise I don't know why the error is occurring. The statement is intended to remove any ad-hoc formatting that may have been applied to the heading sentence. If you don't want formatting removed you can safely remove the statement otherwise surround the statement with an error trap:

On Error Resume Next
Selection.ClearCharacterAllFormatting
On Error GoTo 0


2015-08-26 02:29:31

Esi

I had an error when run this line:
Selection.ClearCharacterAllFormatting


2014-04-15 01:51:37

Ken Endacott

Steve

Seems that you have CTRL/Click turned on.

You need to hold down CTRL when you click the heading.



2014-04-14 18:19:57

steve gray

Clicking on a TOC entry does not go to the proper heading. Clicking does nothing. What am I doing wrong?


2014-04-14 10:49:44

Andy

@Carol - I agree that's a great tip. :)

Regarding the pdf conversion, I suggest using the "build bookmarks using headings" option when saving as pdf, which gives users reading the pdf the ability to navigate the document (and get back to the TOC) really easily.


2014-04-12 18:23:43

Ken Endacott

If the macro HyperlinkHeadings is applied to the document more than once the link references can become incorrect. This means that after running the macro you should not make changes to headings or the Tables of Contents.

The following macro HyperlinkHeadings2 corrects these problems. Headings can be added or deleted and the Table of Contents re-created and running the macro again will resolve the references.

The macro is almost bullet proof. However, if you decide to change the Table of Contents from for example, three levels to two levels you could end up with hyperlinks on level 3 headings having unresolved references. Hence make sure that you have a backup copy.

Sub HyperlinkHeadings2()
Dim hyp As Hyperlink
'Dim h As Hyperlink
Dim toc As TableOfContents
Dim bkmk As String
Dim bkmkR As String
Dim sCode As String
If ActiveDocument.TablesOfContents.Count = 0 Then
MsgBox "There are no Tables of Contents in document"
Exit Sub
End If
Options.CtrlClickHyperlinkToOpen = True
Set toc = ActiveDocument.TablesOfContents(1)
For Each hyp In toc.Range.Hyperlinks
bkmk = hyp.SubAddress
bkmkR = bkmk & "R"
hyp.Range.Select
If Selection.Bookmarks.Count = 0 Then
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=bkmkR
Else
bkmkR = Selection.Bookmarks(1).Name
End If
If ActiveDocument.Bookmarks.Exists(bkmk) Then
ActiveDocument.Bookmarks(bkmk).Range.Select
If Selection.Hyperlinks.Count = 0 Then
With ActiveDocument.Hyperlinks.Add(Anchor:=Selection.Range, _
Address:="", SubAddress:=bkmkR, TextToDisplay:=Selection.Text)
.Range.Select
Selection.ClearCharacterAllFormatting
End With
Else
Selection.Range.Hyperlinks(1).Range.Select
Selection.Fields.Unlink
sCode = Selection.Text
With ActiveDocument.Hyperlinks.Add(Anchor:=Selection.Range, _
Address:="", SubAddress:=bkmkR, TextToDisplay:=sCode)
.Range.Select
Selection.ClearCharacterAllFormatting
End With
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=bkmk
End If
End If
Next hyp
Options.CtrlClickHyperlinkToOpen = False
End Sub


2014-04-12 09:38:03

Ena Shaw

I love this tip. So many times I have found it necessary to edit an error in a heading but had to CTRL+Home and then page down to the TOC.

Thank you!


2014-04-12 06:33:55

Carol

Thanks - that's a handy tip! I guess setting the hyperlink to return to the TOC is useful if you are planing to create a PDF version of the Word document.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.