![]() |
|
|
|
#1
|
||||
|
||||
|
A Table of Contents in Word automatically hyperlinks to the corresponding headings - provided you've set things up correctly. The simplest way to do the setup is to use Word's heading Styles; the Table of Contents (TOC) field automatically picks those up. Accordingly, I don't follow what you mean by: Quote:
Since a Word Table of Contents won't work in the ebook format, what you could do is to use a Word Table of Contents for editing purposes and, when you're done convert it to hyperlinks, which is what the following macro does: Code:
Sub ConvertTOC2Hyperlinks()
Dim RngTOC As Range, RngItem As Range, StrBkMkList As String, StrTmp As String, i As Long
With ActiveDocument
With .TablesOfContents(1)
.Update
If InStr(.Range.Fields(1).Code.Text, "\n") = 0 Then
For i = 3 To .Range.Fields.Count Step 2
StrBkMkList = StrBkMkList & "|" & Split(Trim(.Range.Fields(i).Code.Text), " ")(1)
Next
Else
For i = 2 To .Range.Fields.Count
StrBkMkList = StrBkMkList & "|" & Replace(Split(Trim(.Range.Fields(i).Code.Text), " ")(2), """", "")
Next
End If
Set RngTOC = .Range
End With
RngTOC.Fields.Unlink
For i = 1 To UBound(Split(StrBkMkList, "|"))
Set RngItem = RngTOC.Paragraphs(i).Range
RngItem.End = RngItem.End - 1
StrTmp = Replace(Split(StrBkMkList, "|")(i), "Toc", "HL")
.Bookmarks.Add Name:=StrTmp, Range:=.Bookmarks(Split(StrBkMkList, "|")(i)).Range
.Bookmarks(Split(StrBkMkList, "|")(i)).Delete
.Hyperlinks.Add Anchor:=RngItem, SubAddress:=.Bookmarks(StrTmp).Range, _
TextToDisplay:=.Bookmarks(StrTmp).Range.Text
Next
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#2
|
|||
|
|||
|
Hi Macro & many thanks for your reply.
To clarify the bit you say you don't understand - I literally just typed the document from start to finish, no formatting. Then typed a contents page at the start with a list of the chapters. Then simply connected the corresponding chapters to their counterparts in the contents page using the Bookmarks/Hyperlinks method I describe. So a very manual process in an unformatted document (which is why I guess it didn't work.) Macros are most definitely way over my head, as you can probably tell from the ham-fisted way I did my hyperlinks. So if you could dumb it down a bit I'd be grateful. R Last edited by macropod; 09-19-2015 at 10:39 PM. Reason: Deleted superfluous quote of entire post replied to |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Page Number Formatting Question | Redgum | Word | 3 | 09-17-2015 04:08 PM |
Drop down menus colour formatting - Newbie question
|
TerryStevenson | Word | 2 | 09-17-2013 10:43 AM |
Conditional formatting question based on cell date
|
Cosmo | Excel | 2 | 04-08-2013 12:12 PM |
Formatting question...
|
rwear | Word | 1 | 01-30-2011 11:36 PM |
| Excel 2007 - Question on conidial formatting? | jerrymbna22 | Excel | 2 | 03-24-2010 11:48 AM |