TablesOfContents Property
TOC
Collapse the table of content
Expand the table of content

DocumentBase.TablesOfContents Property

 

Gets a TablesOfContents collection that represents the tables of contents in the document.

Namespace:   Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v4.0.Utilities (in Microsoft.Office.Tools.Word.v4.0.Utilities.dll)

public TablesOfContents TablesOfContents { get; }

Property Value

Type: Microsoft.Office.Interop.Word.TablesOfContents

A TablesOfContents collection that represents the tables of contents in the document.

The following code example adds two paragraphs to the document and assigns the Heading 1 and Heading 2 styles to the paragraphs. The code then creates a table of contents at the insertion point that includes only paragraphs styled Heading 1. To use this example, run it from the ThisDocument class in a document-level project.

private void DocumentTablesOfContents()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.Text = "Heading 1";
    this.Paragraphs[2].Range.Text = "Heading 2";

    object Style1 = Word.WdBuiltinStyle.wdStyleHeading1;
    this.Paragraphs[1].set_Style(ref Style1);

    object Style2 = Word.WdBuiltinStyle.wdStyleHeading2;
    this.Paragraphs[2].set_Style(ref Style2);

    object HeadingLevel = 1;
    this.TablesOfContents.Add(this.Application.Selection.Range,
        ref missing, ref HeadingLevel, ref HeadingLevel,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing);
}
Return to top
Show:
© 2016 Microsoft