Table of Contents

You can have more than one table of contents in a document since it is a collection object.


TablesOfContents object

ActiveDocument.TablesOfContents.Add Range:=Selection.Range, _ 
                                    UseHeadingStyles:=True, _
                                    UpperHeadingLevel:=1, _
                                    LowerHeadingLevel:=3, _
                                    UseFields:=False, _
                                    TableID:=False, _
                                    RightAlignPageNumbers:=True, _
                                    IncludePageNumbers:=True, _
                                    AddedStyles:="", _
                                    UseHyperlinks:=True, _
                                    HidePageNumbersInWeb:=True, _
                                    UseOutlineLevels:=True

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.
UseHeadingStyles - (Variant). True to use built-in heading styles to create the table of contents. The default value is True.
UpperHeadingLevel - (Variant) 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 - (Variant) 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 - (Variant). 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 (Variant) - 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 - (Variant). True if page numbers in the table of contents are aligned with the right margin. The default value is True.
IncludePageNumbers (Variant). True to include page numbers in the table of contents. The default value is True.
AddedStyles - (Variant). 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 (Variant). 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 (Variant). 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 (Variant). True to use outline levels to create the table of contents. The default is False.



The following example inserts a table of contents entry that references the selected text in the active document.

ActiveDocument.TablesOfContents.MarkEntry Range:=Selection.Range, _ 
                                          Level:=2, _
                                          Entry:="Introduction"


Use the Add method to add a table of contents to a document. The following example adds a table of contents at the beginning of the active document.
The example builds the table of contents from all paragraphs styled as either Heading 1, Heading 2, or Heading 3.

Set myRange = ActiveDocument.Range(Start:=0, End:=0) 
ActiveDocument.TablesOfContents.Add Range:=myRange, _
                                    UseFields:=False, _
                                    UseHeadingStyles:=True, _
                                    LowerHeadingLevel:=3, _
                                    UpperHeadingLevel:=1
                                    AddedStyles:="myStyle, yourStyle"

The following example updates the page numbers of the items in the first table of figures in the active document.

ActiveDocument.TablesOfContents(1).UpdatePageNumbers 

This example adds a table of contents at the beginning of Sales.doc. The table of contents collects entry text from TC fields.

Set myRange = Documents("Sales.doc").Range(Start:=0, End:=0) 
Documents("Sales.doc").TablesOfContents.Add Range:=myRange, _
                                            UseFields:=True, _
                                            UseHeadingStyles:=False


This example updates the page numbers for items in the table of contents in the active document.

For Each myTOC In ActiveDocument.TablesOfContents 
    myTOC.UpdatePageNumbers
Next myTOC


Specifies the type of formatting to apply to the table of contents

Application.ActiveDocument.TableOfContents.Format = wdTocFormat.wdTOCTemplate 


Dim oToC As Word.TableOfContents 
Set oTOC = Documents("Sales.doc").TablesOfContents.Add()
oToC.TabLeader = wdTabLeader.wdTabLeaderSpaces (this is the default)




© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext