Class TableOfContents (Word VBA)

The class TableOfContents represents a single table of contents in a document. The TableOfContents object is a member of the TablesOfContents collection. The TablesOfContents collection includes all the tables of contents in a document.

The main procedures of class TableOfContents are Delete and TablesOfContents.Add

Set

To use a TableOfContents class variable it first needs to be instantiated, for example

Dim tocs as TableOfContents
Set tocs = ActiveDocument.TablesOfContents(Index:=1)

The following procedures can be used to set variables of type TableOfContents: TablesOfContents.Add, TablesOfContents.Item and Document.TablesOfContents

For Each

Here is an example of processing the TableOfContents items in a collection.

Dim hs As HeadingStyle 
If ActiveDocument.TablesOfContents.Count >=1 Then 
    ActiveDocument.TablesOfContents(1).HeadingStyles.Add Style:="Title", Level:=2 
 For Each hs In _ 
 ActiveDocument.TablesOfContents(1).HeadingStyles 
 MsgBox hs.Style 
 Next hs 
End If

Themes

The following procedures in this class have been grouped together and are described on a separate theme page

Page with its procedures IncludePageNumbers, RightAlignPageNumbers and UpdatePageNumbers

Methods

These are the main methods of the TableOfContents class

Delete - Deletes the specified table of contents.

ActiveDocument.TablesOfContents(1).Delete

TablesOfContents.Add - Returns a TableOfContents object that represents a table of contents added to a document.

Dim tocs As TableOfContents
Set tocs = ActiveDocument.TablesOfContents.Add(Range:=)

Other Methods

TablesOfContents.MarkEntry - Inserts a TC (Table of Contents Entry) field after the specified range. The method returns a Field object representing the TC field.

Dim fldMarkEntry As Field
Set fldMarkEntry = ActiveDocument.TablesOfContents.MarkEntry(Range:=)

Properties

HeadingStyles returns a HeadingStyles object that represents additional styles used to compile a table of contents or table of figures (styles other than the Heading 1 - Heading 9 styles).

Dim hssHeadingStyles As HeadingStyles
Set hssHeadingStyles = ActiveDocument.TablesOfContents(1).HeadingStyles

HidePageNumbersInWeb returns or sets whether page numbers in a table of contents or a table of figures should be hidden when publishing to the Web.

ActiveDocument.TablesOfContents(1).HidePageNumbersInWeb = True

LowerHeadingLevel returns or sets the ending heading level for a table of contents or table of figures.

ActiveDocument.TablesOfContents(1).LowerHeadingLevel =

Parent returns an object that represents the parent object of the specified TableOfContents object.

Dim objParent As Object
Set objParent = ActiveDocument.TablesOfContents(1).Parent

Range returns a Range object that represents the portion of a document that is contained within the specified table of contents.

Dim rngRange As Range
Set rngRange = ActiveDocument.TablesOfContents(1).Range

TabLeader returns or sets the character between entries and their page numbers in an index, table of authorities, table of contents, or table of figures.

ActiveDocument.TablesOfContents(1).TabLeader = wdTabLeaderDashes

TableID returns or sets a one-letter identifier that's used to build a table of contents from TOC fields.

ActiveDocument.TablesOfContents(1).TableID =

UpperHeadingLevel returns or sets the starting heading level for a table of contents.

ActiveDocument.TablesOfContents(1).UpperHeadingLevel =

UseFields true if Table of Contents Entry (TC) fields are used to create a table of contents or a table of figures.

ActiveDocument.TablesOfContents(1).UseFields = True

UseHeadingStyles true if built-in heading styles are used to create a table of contents.

ActiveDocument.TablesOfContents(1).UseHeadingStyles = True

UseHyperlinks returns or sets whether entries in a table of contents should be formatted as hyperlinks when publishing to the Web.

ActiveDocument.TablesOfContents(1).UseHyperlinks = True

TablesOfContents.Count returns a Long that represents the number of tables of contents in the collection.

Dim lngCount As Long
lngCount = ActiveDocument.TablesOfContents.Count

TablesOfContents.Format returns or sets the formatting for the tables of contents in the specified document.

ActiveDocument.TablesOfContents.Format = wdTOCClassic

TablesOfContents.Parent returns an object that represents the parent object of the specified TablesOfContents object.

Dim objParent As Object
Set objParent = ActiveDocument.TablesOfContents.Parent