Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

Adding hyperlinks in a manual TOC in word 2010

Hi EEE,

  I have a word doc 2010. I have a manual TOC set up and needed to hyperlink the titles involved in the toc to relevant sections of the doc. How can I accomplish this?

I dont want to do an auto TOC as it will involve a lot of work at this point.

Thx
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

What do you mean by 'a manual TOC'? Can you post a sample document, please?
I think you have to use bookmarks!
Insert tab -> Links -> Bookmark

Go to each title and insert a bookmark with a unique name (alphanumeric, no spaces, can't START with a number - ex.: name1, NOT 1name, NOT name 1, NOT name.1).

Go back to the manual TOC and insert a hyperlink (Insert tab -> Links -> Hyperlink) in each title. Select the entire line!

Select the Place in This Document in the options list on the left-hand side of the and choose the Bookmark name corresponding to the place in the document you inserted it in.

Here is an example.
Manual-TOC.docx
It is hard to imagine how using an auto TOC could be MORE work... worst case scenario is heading styles have not been used?  

For the time taken to manually add each link I'd put my money on applying the heading styles manually... and creating the TOC automatically.  Failing that, I 2nd Graham's call for a sample doc!
ASKER CERTIFIED SOLUTION
Avatar of Paul Sauvé
Paul Sauvé
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes the built-in TOC functionality in Word is much easier, but here is some VBA code that sketches out a method for doing what you ask.

It assumes that your TOC is a block of text paragraphs at the start of the document, and that the block is contained in a bookmark named MyToc.

It looks for the first occurrence after the TOC of each piece of text. It bookmarks that location and turns the TOC entry into a hyperlink to the bookmark.
Sub MakeHyperLinks()
    Dim Para As Paragraph
    Dim rngPara As Range
    Dim rngContent As Range
    Dim iBmkNumber As Integer
    
    For Each Para In ActiveDocument.Bookmarks("MyToc").Range.Paragraphs
        Set rngPara = Para.Range
        rngPara.MoveEnd wdCharacter, -1 'drop paragraph mark
        Set rngContent = ActiveDocument.Range
        rngContent.Start = rngPara.End + 1
        iBmkNumber = iBmkNumber + 1
        With rngContent.Find
            .Text = rngPara.Text
            If .Execute Then
                ActiveDocument.Bookmarks.Add "bmkTocEntry" & iBmkNumber, rngContent
                ActiveDocument.Hyperlinks.Add rngPara, , "bmkTocEntry" & iBmkNumber, , rngPara.Text
            End If
        End With
    Next Para
End Sub

Open in new window

Paulsauve - Agreed. Important to answer the users question... Sometimes I have asked similar questions.  More than once I've felt the need to do things differently to 'how Word works' (other times I was simply unaware of what Word can do).  Still... hard to imagine, I'd be curious to see a sample.

Going down the 'how Word works' path... an alternate approach could be an adaptation of Grahams code to search for matching text based on the TOC and applying the appropriate heading style in the document...

I assume that would be an acceptable amount of work?