Written by Allen Wyatt (last updated February 20, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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 specific shortcut for accomplishing this task is Alt+Left Arrow. The shortcut jumps 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:
Figure 1. The Quick Access Toolbar area of the Word Options dialog box.
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
Note:
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, 2013, 2016, 2019, and Word in Microsoft 365.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If your document is any length at all, adding a table of contents is a nice touch. This tip demonstrates how easy it is ...
Discover MoreWord makes it easy to create a Table of Contents. If you want column headings in that table, getting them takes a bit of ...
Discover MoreAdding multiple tables of contents is a must for some types of document design. Here's a great overview of how you can ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-07-18 21:41:13
nijineko
This macro is exactly what I was looking for!
Sadly, I do not have Word, but instead use LibreOffice.
Would you happen to know of anyone that could convert this macro to a LibreOffice compatible macro, by chance?
In any case, thank you for this work.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments