How to determine index of new TOC on the fly?

C

Chuck

I've got a sub that allows users to insert a new Table of Contents (TOC) anywhere in a document. There may be no existing TOCs or more than one.

How should the code refer to the TOC that the sub inserts? How can I determine the index number of the TOC that my code inserts when there may be any number of existing TOCs?

For example, say there are two existing TOCs in the current document, TOC(1) and TOC(2). The user wants to insert a new TOC between TOC(1) and TOC(2). I'm assuming that the code cannot specify "ActiveDocument.TableOfContents(2)" because the code has to allow for situations where the new TOC will be inserted before TOC(1) or after TOC(2), or after TOC(5) for that matter.

Using the sample code below, what number/variable would I replace "???" with?

With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:="Level1,1,Level2,2" 'style names examples only
.TablesOfContents(???).TabLeader = wdTabLeaderDots
End With

Hope this question is clear, and thanks for any help!
 
C

Chuck

The MS message board system gave me an error saying that the first message hadn't been posted.
 
D

Doug Robbins

Use:

Dim mytoc As TableOfContents
Set mytoc = Documents.Add.TablesOfContents

then refer to mytoc
--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
Chuck said:
I've got a sub that allows users to insert a new Table of Contents (TOC)
anywhere in a document. There may be no existing TOCs or more than one.
How should the code refer to the TOC that the sub inserts? How can I
determine the index number of the TOC that my code inserts when there may be
any number of existing TOCs?
For example, say there are two existing TOCs in the current document,
TOC(1) and TOC(2). The user wants to insert a new TOC between TOC(1) and
TOC(2). I'm assuming that the code cannot specify
"ActiveDocument.TableOfContents(2)" because the code has to allow for
situations where the new TOC will be inserted before TOC(1) or after TOC(2),
or after TOC(5) for that matter.
 

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

Similar Threads

TOC problem 0
Table of Contents Nightmare 1
TOC Problem 0
TOC Help 0
Table of Content 1
Table of Contents Styles font issue 5
TOC Question 3
How to refer to specific TOC? 1

Top