How to refer to specific TOC?

C

Chuck

A general question about how to refer to a specific TOC (table of contents) in a document:

If a sub adds a TOC to a document that may already have one or more than one TOC, is there an easy way to either determine that TOC's index or assign it a tag of some sort? We have documents that can contain any multiple (different) TOCs in anywhere in the document.

For example, if the user may need to insert a TOC ("NEW-TOC") after TOC(1) but before current TOC(2). For the purposes of working with NEW-TOC, what value/variable goes in the index position in the following line, bearing in mind that the code has to accommodate adding a TOC anywhere in a document where the number of TOCs isn't known ahead of time:

With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, _
RightAlignPageNumbers:= True, _
UseHeadingStyles:=False, _
IncludePageNumbers:=True, _
AddedStyles:="Level 1,1,Level 2,2" 'style names as examples only
.TablesOfContents(NEW-TOC).TabLeader = wdTabLeaderDots
End With

I know I can't specify "2" in place of "NEW-TOC" in the above sample code, because the code also has to work if the user is inserting the TOC after current TOC(2) or before current TOC(1). The user picks the insertion point for NEW-TOC as needed.

I hope this question is clear and appreciate any assitance. Thanks!
 
W

Word Heretic

G'day "Chuck" <[email protected]>,

The .Add returns an object, so use it, eg

Dim MyTOC as TableOfContents
With ActiveDocument
Set MyTOC = .TablesOfContents.Add Range:=Selection.Range,

....

Now we can grab its range and get your index for you:

Dim Finder as Range
Dim TOCIndex as Long
....

Set Finder = MyTOC.Range

'Now move the start to the doc start and count the TOCs!

Finder.MoveStart wdstory,-1

TOCIndex=Finder.TablesOfContents.Count


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Chuck reckoned:
 

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
TOC Question 3
Table of Contents Styles font issue 5
How to determine index of new TOC on the fly? 2

Top