Class TablesOfContents (Word VBA)
A collection of TableOfContents objects that represent the tables of contents in a document. To use a TablesOfContents class variable it first needs to be instantiated, for example
Dim tocs as TablesOfContents
Set tocs = ActiveDocument.TablesOfContents
For Each
Here is an example of processing the TablesOfContents items in a collection.
Dim tocsTablesOfContent As TableOfContents
For Each tocsTablesOfContent In ActiveDocument.TablesOfContents
Next tocsTablesOfContent
Add
Returns a TableOfContents object that represents a table of contents added to a document.
Add (Range, UseHeadingStyles, UpperHeadingLevel, LowerHeadingLevel, UseFields, TableID, RightAlignPageNumbers, IncludePageNumbers, AddedStyles, UseHyperlinks, HidePageNumbersInWeb, UseOutlineLevels)
Set myRange = ActiveDocument.Range(0, 0)
ActiveDocument.TablesOfContents.Add _
Range:=myRange, _
UseFields:=False, _
UseHeadingStyles:=True, _
LowerHeadingLevel:=3, _
UpperHeadingLevel:=1, _
AddedStyles:="myStyle, yourStyle"
Arguments
The following argument is required
Range (Range) - The range where you want the table of contents to appear. The table of contents replaces the range, if the range isn't collapsed.
Optional arguments
The following arguments are optional
UseHeadingStyles (Boolean) - True to use built-in heading styles to create the table of contents. The default value is True.
UpperHeadingLevel (Long) - The starting heading level for the table of contents. Corresponds to the starting value used with the \o switch for a Table of Contents (TOC) field. The default value is 1.
LowerHeadingLevel (Byte) - The ending heading level for the table of contents. Corresponds to the ending value used with the \o switch for a Table of Contents (TOC) field. The default value is 9.
UseFields (Boolean) - True if Table of Contents Entry (TC) fields are used to create the table of contents. Use the MarkEntry method to mark entries to be included in the table of contents. The default value is False.
TableID (String) - A one-letter identifier that's used to build a table of contents from TC fields. Corresponds to the \f switch for a Table of Contents (TOC) field. For example, "T" builds a table of contents from TC fields using the table identifier T. If this argument is omitted, TC fields aren't used.
RightAlignPageNumbers (Boolean) - True if page numbers in the table of contents are aligned with the right margin. The default value is True.
IncludePageNumbers (Boolean) - True to include page numbers in the table of contents. The default value is True.
AddedStyles (HeadingStyles) - The string name for additional styles used to compile the table of contents (styles other than the Heading 1 – Heading 9 styles). Use the Add method of a HeadingStyles object to create new heading styles.
UseHyperlinks (Boolean) - True if entries in a table of contents should be formatted as hyperlinks when the document is being publishing to the web. The default value is True.
HidePageNumbersInWeb (Boolean) - True if page numbers in a table of contents should be hidden when the document is being publishing to the web. The default value is True.
UseOutlineLevels (Boolean) - True to use outline levels to create the table of contents. The default is False.
Count
Returns a Long that represents the number of tables of contents in the collection.
Dim lngCount As Long
lngCount = ActiveDocument.TablesOfContents.Count
Format
Returns or sets the formatting for the tables of contents in the specified document. Possible return values are wdTOCClassic - Classic formatting, wdTOCDistinctive - Distinctive formatting, wdTOCFancy - Fancy formatting, wdTOCFormal - Formal formatting, wdTOCModern - Modern formatting, wdTOCSimple - Simple formatting, wdTOCTemplate - Template formatting.
Documents("Report.doc").TablesOfContents.Format = wdTOCClassic
Item
Returns an individual TableOfContents object in a collection.
Item (Index)
Index: The individual object to be returned. Can be a Long indicating the ordinal position of the individual object.
Dim tocsTablesOfContent As TableOfContents
Set tocsTablesOfContent = ActiveDocument.TablesOfContents(Index:=1)
MarkEntry
Inserts a TC (Table of Contents Entry) field after the specified range. The method returns a Field object representing the TC field.
MarkEntry (Range, Entry, EntryAutoText, TableID, Level)
Dim fldMarkEntry As Field
Set fldMarkEntry = ActiveDocument.TablesOfContents.MarkEntry(Range:=)
Arguments
The following argument is required
Range (Range) - The location of the entry. The TC field is inserted after Range.
Optional arguments
The following arguments are optional
Entry (Table) - The text that appears in the table of contents or table of figures. To indicate a subentry, include the main entry text and the subentry text, separated by a colon (:) (for example, "Introduction:The Product").
EntryAutoText (String) - The AutoText entry name that includes text for the index, table of figures, or table of contents (Entry is ignored).
TableID (String) - A one-letter identifier for the table of figures or table of contents item (for example, "i" for an "illustration").
Level (Long) - A level for the entry in the table of contents or table of figures.