Anyway to prevent those hyperlinks in the TOC (aside from deleting the h switches after the fact)?
Thanks.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » TOC Page Number Hyperlinks
Hi,
Are you talking about a Word 2000 TOC? – I haven’t been using Word 2000 for too long and haven’t played around with these much yet, but it looks like the issue is that they behave differently from Word 97 TOCs – in a Word 97 TOC, only the page number reference is an active link back to a location in the document proper, while in Word 2000 TOCs each line reference in its entirety is a hyperlink.
Here’s a macro which will delete the hyperlinks in a Word 2000 TOC, turning it into a ‘Word 97-style’ TOC:
Sub RemoveHLinksFromTOC() 'Gary Frieder January 2001 'Purpose: Changes a Word 2000 TOC to a "Word 97-type" TOC ' i.e. only the TOC page reference is a link Dim rngTOCRng As Range Dim n As Long Set rngTOCRng = ActiveDocument.TablesOfContents(1).Range 'Remove hyperlinks For n = rngTOCRng.Hyperlinks.Count To 1 Step -1 rngTOCRng.Hyperlinks(n).Delete Next n 'Remove the "remmant" blue underscore: With rngTOCRng.Font .Underline = wdUnderlineNone .Color = wdColorBlack End With End Sub
Hope this helps,
Gary
Thanks Gary, you are correct, this never happened in Office 97. I will try the macro. I have discovered through trial and error that there are hidden bookmarks in the Hyperlink fields in my TOC. I went to Insert/Bookmarks and clicked on Hidden Bookmarks and they are all there.
Thanks for the macro.
I suspect that deleting the l switch is easier. I like the hyperlinks for documents that I use and edit online. (In Word 2000 they don’t show up with underlines.) If what you are after is a printed document, though, and you are printing in Word 97 it looks bad. Another alternative, if you don’t have any other hyperlinks in your document (or even if you do) is to change the hyperlink character style to get rid of the color and underline.
However, if you are printing in Word 97, you can update the TOC field, in which case the hyperlinks will disappear because Word 97 can’t insert them.
Sorry, I should have said the h switch instead of the l switch. The h switch is the one in the TOC field rather than the one in the individual entry listings in the TOC.
{TOC h z t “Heading 2,2,Heading 2a,2”} is a TOC field.
{HYPERLINK l “_Toc501741425”} is what I get if with field results (the TOC) displayed, I move the insertion point inside the TOC and press Shift-F9 to toggle the field codes. The rest of the TOC is still there and only the first line is changed.
Here’s another method. I first used For Each…Next with each TOC object, but this caused an infinite loop, for some reason. So, I fell back on the less elegant For…Next:
Sub NukeTOCHyperlinks()
Dim colTOCs As TablesOfContents, intCount As Integer
Set colTOCs = ActiveDocument.TablesOfContents
For intCount = 1 To colTOCs.Count
colTOCs(intCount).UseHyperlinks = False
Next
End Sub
I guess someone enterprising could add a MsgBox or make it a toggle…
> Are there advantages or disadvantages using this macro vs. Gary’s above?
I didn’t test it to see. I just thought it simpler to operate on a global property (simpler except for the time it took to find it and program it). One possible issue: if the global property is not changed, could a rebuild of the TOC bring the hyperlinks back?
As a rule of thumb, the more direct approach is usually better, since it’s simpler, runs more quickly etc.
Jefferson’s version is more direct, in using the UseHyperlinks property. (I probably would have used it too, if I’d dug a little deeper when doing my version – there’s lots of good stuff buried in the object model.)
The main difference that comes out in testing them is reversibility. Apparently setting UseHyperlinks to False permanently removes the hyperlink capability from that TOC. Updating the TOC won’t bring them back. So if you want to nuke the hyperlinks and know you never want them back, then that is the way to go.
The method I used (of deleting hyperlinks) turns out to be reversible; if you update the TOC the hyperlinks come back. This could be desirable or not, depending on your proclivities.
Hi Gary:
Thanks for the explanation. I prefer the approach that keeps the TOC the same after updating–otherwise I’d have to redo it each time. I’d like to modify it though to include TOFs, like you did with your other macro. However, if they’re to be in one macro, I’d need an If…Then…Else approach in case there were no TOFs. Otherwise, I notice that I get an error that “a member of the collection does not exist”.
Hi Phil,
You might want to put in an If test for both TOCs and TOF:
(adapted from Jefferson’s original:)
Sub NukeTOCTOFHyperlinks() Dim colTOCS As TablesOfContents Dim colTOFS As TablesOfFigures Dim intCount As Integer Set colTOCS = ActiveDocument.TablesOfContents Set colTOFS = ActiveDocument.TablesOfFigures For intCount = 1 To colTOCS.Count If intCount > 0 Then colTOCS(intCount).UseHyperlinks = False End If Next For intCount = 1 To colTOFS.Count If intCount > 0 Then colTOFS(intCount).UseHyperlinks = False End If Next End Sub
Gary
I would be surprised if the TOF part is needed anyway. Both a standard TOC and the specialised TOF are both TOCs. Have a look at their field codes and the only difference between them is the switches and what they are told to build on.
Step through the first part and see if the TOF is processed as part of the first loop (it should be as it is a TOC). If it isn’t then have a look at moving the Set command for the TOF after the first loop. I suspect that the first loop removes the item on which the second ones loop counter relies on.
Hi Phil,
I don’t have access right now to the version I posted some time ago on DWT – which did work for both TOCs and TOFs (I think – or was it TOAs!). That one used Hyperlinks.Delete rather than UseHyperlinks = False.
Maybe what could be done is to first do the TOCs using the UseHyperlinks = False method, and then do the TOFs using the Hyperlinks.Delete method.
If you happen to have a copy of that code, post it, and we can try to patch the two parts together. Otherwise I can try to locate it on my work PC tomorrow.
Gary
Hi Gary:
I know I’m a couple of months behind, but I still haven’t been able to solve this one. I’d like to get a macro that would permanently (i.e. through updates) remove the Word 2000 hyperlink from both the TOC & TOF. Since it’s one macro & therefore, it would have to test if there were in fact any TOCs or TOFs in the document. I tried doing this with your macro, but couldn’t. Any way to do this?
This is the macro that you had:
Sub RemoveHLinksFromTOC_TOF
Dim rngTOCRng As Range
Dim rngTOFRng As Range
Dim n As Long
Dim c As Long
Set rngTOCRng = ActiveDocument.TablesOfContents(1).Range
Set rngTOFRng = ActiveDocument.TablesOfFigures(1).Range
‘Remove hyperlinks from TOC:
For n = rngTOCRng.Hyperlinks.Count To 1 Step -1
rngTOCRng.Hyperlinks(n).Delete
Next n
‘Remove the “remnant” blue underscore:
With rngTOCRng.Font
.Underline = wdUnderlineNone
.Color = wdColorBlack
End With
‘Remove hyperlinks from TOF:
For c = rngTOFRng.Hyperlinks.Count To 1 Step -1
rngTOFRng.Hyperlinks©.Delete
Next c
‘Remove the “remnant” blue underscore:
With rngTOFRng.Font
.Underline = wdUnderlineNone
.Color = wdColorBlack
End With
Thanks again.
Hi Phil,
– I know exactly how you feel!
Determining whether a document has any TOCs or TOFs should be pretty straightforward:
If ActiveDocument.TablesOfContents.Count > 0 Then
‘do stuff
– and the approach would be the same for TOFs.
Note that you’d want to put each of these tests before the respective “Set rngTOCRng = etc.”, because if the document actually did not contain a TOC (or TOF), this statement would cause an error.
Hi Gary:
I just thought I’d let you know that Pieter Janssens developed a macro that removes hyperlinks from both TOC & TOF. If one or the other doesn’t exist, there are no error messages.
RemoveHyperLinksFromTOC_TOF
Dim TOC As TableOfContents
Dim TOF As TableOfFigures
With ActiveDocument
For Each TOC In .TablesOfContents
With TOC.Range
Do Until .Hyperlinks.Count = 0
.Hyperlinks(1).Delete
Loop
With .Font
.Underline = wdUnderlineNone
.Color = wdColorBlack
End With
End With
Next
For Each TOF In .TablesOfFigures
With TOF.Range
Do Until .Hyperlinks.Count = 0
.Hyperlinks(1).Delete
Loop
With .Font
.Underline = wdUnderlineNone
.Color = wdColorBlack
End With
End With
Next
End With
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications