Modifying/Inserting hyperlinks into TOC

V

Val

I have a multi-file document which I've created a TOC
using RD statements (I'm avoiding master documents).
Unfortunately, I haven't been able to figure out how to
get the TOC to generate with hyperlinks (since it is
multifile I think this is fruitless). I have a Macro that
will create bookmarks at each heading. What I'd like to
do is insert hyperlinks that reference the correct file
and bookmark and I'd like this to be done with a Macro.
The TOC looks like this
1 Intro ............... 1-1
1.1 Sectionllll ..... 1-2
1.2 Sectionllkdjs ... 1-6
2 chpater 2 ........... 2-1
2.1 secljsljdlf ..... 2-3

and so on. I've created my bookmarks to be Bookmark_1,
Bookmark_1_1, Bookmark_1_2, Bookmark_2, .. etc. My
problem is I haven't figured out how to reference each
line in the TOC. I can select the entire TOC but not a
single line. I'd like to be able to select a line, parse
it, and insert a hyperlink with out changing the displayed
text. By parsing the line I can then tell it what
filename and bookmark to use in the hyperlink. For
example all of section 1 is in file(1) so the first line
should have a hyperlink pointing to file(1)#bookmark_1

Is the above possible????? I can't figure out how to
parse a single line in the TOC.

below is what I have so far

Thanks,

Val

Public Sub Insert_Hyperlinks_into_TOC()
' for Each bookmark inserted create hyperlink in TOC to it
' separate out Figure & Table styles
' when creating hyperlink insert filename prior to
bookmark reference
' use defined styles for TOC

' Run Macro from existing TOC file

Dim x As String

Selection.HomeKey Unit:=wdStory, Extend:=wdMove 'moves
cursor to beginning of document

' Find Table Of Contents
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Table of Contents"
.Replacement.Text = ""
.Forward = True
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=1
' move to beginning of line and select entire line
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

x = Selection.Text

Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
Selection.Range.Hyperlinks(1).Delete
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:= _
"D:\test.doc", SubAddress:="_Bookmark_2", _
TextToDisplay:="display_text"
Selection.Collapse Direction:=wdCollapseEnd
Selection.style = ActiveDocument.Styles("TOC 2")

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top