# Tables of Contents

> Generate a table of contents from structure levels with TableOfContentsDialog, then update, change and delete it in VB6 as the TXWords sample does.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Tables of Contents
- **2441 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/tables-of-contents/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/tables-of-contents/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/tables-of-contents/llms-full.txt

---

How does `TXTextControl` know which paragraphs belong in a table of contents? Each paragraph has a *structure level*: 0 for body text, 1 to 10 for headings of different depth. A table of contents lists the paragraphs of the levels you choose, optionally with page numbers and links to the headings, and TX Text Control generates it for you. Tables of contents are available since version 29.0.

Users insert and change a table of contents in a built-in dialog box; your Visual Basic 6 code opens the dialog box, updates and deletes tables of contents and follows the text input position in and out of them. The samples come from the *TXWords* sample program, which [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/llms-full.txt) describes.

### How a Table of Contents Is Generated

A table of contents is built from three things:

- **Structure levels.** The built-in paragraph dialog box, which the [ParagraphDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/paragraphdialog-method/llms-full.txt) method opens, sets the structure level of the selected paragraphs, from 0 to 10. A level can be set for each paragraph, but it is more common to make it an attribute of a paragraph style, so that every heading of one style has the same level. [Stylesheets](https://docs.textcontrol.com/tx-text-control-activex/document-editor/stylesheets/llms-full.txt) describes styles and the style dialog box.
- **The attributes of the table of contents.** In the dialog box that [TableOfContentsDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdialog-method/llms-full.txt) opens, the user chooses the lowest and the highest structure level that the table of contents includes, a title and whether it shows page numbers.
- **The paragraphs of the document.** TX Text Control searches the document for all paragraphs with the chosen structure levels and inserts the table of contents at the *text input position*, the place of the caret.

The entries take the font and paragraph attributes of the paragraphs they come from. For each level, TX Text Control also creates a formatting style, named *TOC\_Title*, *TOC\_Level1*, *TOC\_Level2* and so on, so that users change the look of the table of contents by changing these styles.

![A table of contents generated in the TXWords demo document, with dotted tab leaders and right-aligned page numbers.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/toc-document.bd49ac9c.webp)

Links from the entries to the headings can be generated automatically. Each entry is then a marked text field of the [FieldType](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/fieldtype-property/llms-full.txt) `txFieldInternalLink` that points to a link target at its heading. When the user clicks an entry while pressing the CTRL key, the document scrolls to the page of the heading. [Hypertext Links](https://docs.textcontrol.com/tx-text-control-activex/document-editor/fields/hyperlinks/llms-full.txt) describes internal links.

A document can contain several tables of contents, each built from paragraphs of different structure levels.

### Insert a Table of Contents

`TableOfContentsDialog` opens the built-in dialog box. When the text input position isn't in a table of contents, the method inserts a new one with the attributes the user sets:

```vbscript
Dim TOCId As Long
TOCId = TXTextControl1.TableOfContentsDialog
If TOCId = 0 Then
   MsgBox "No table of contents was inserted."
End If
```

The code works as follows:

- `TableOfContentsDialog` returns the unique identifier of the inserted table of contents, a Long value that the other methods take as their parameter.
- It returns 0 when the user left the dialog box with **Cancel**, or when the table of contents would be empty because the document has no paragraph with the chosen structure levels.

![The built-in Table of Contents dialog box of TXTextControl with the minimum and maximum structure levels, the page number options, the tab leader, the hyperlink option and the title.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/toc-dialog.ab48948d.webp)

In TXWords, **Insert** > **Table of Contents...** calls the method without evaluating the return value. TXWords enables the menu item only when the text input position isn't in a table of contents, which it knows from the events in the next section:

```vbscript
Private Sub mnuInsert_TOC_Click()
    TXTextControl1.TableOfContentsDialog
End Sub
```

### Track the Table of Contents at the Input Position

Four events report what happens to tables of contents. Each passes the identifier of the table of contents:

- [TableOfContentsCreated](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentscreated-event/llms-full.txt) occurs when a table of contents has been created.
- [TableOfContentsDeleted](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdeleted-event/llms-full.txt) occurs when one has been deleted.
- [TableOfContentsEntered](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsentered-event/llms-full.txt) occurs when the text input position has moved into a table of contents.
- [TableOfContentsLeft](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsleft-event/llms-full.txt) occurs when it has left one.

TXWords stores the identifier of the table of contents at the text input position in a form-level variable, `CurrentTOCId`, which is 0 while the text input position is elsewhere:

```vbscript
Dim CurrentTOCId As Long

Private Sub TXTextControl1_TableOfContentsEntered(ByVal TOCId As Long)
    CurrentTOCId = TOCId
End Sub

Private Sub TXTextControl1_TableOfContentsLeft(ByVal TOCId As Long)
    CurrentTOCId = 0
End Sub

Private Sub TXTextControl1_TableOfContentsDeleted(ByVal TOCId As Long)
    If CurrentTOCId = TOCId Then CurrentTOCId = 0
End Sub
```

With this variable, TXWords enables **Insert** > **Table of Contents...** only while `CurrentTOCId` is 0, and the **Edit** > **Table of Contents** submenu only while it isn't.

### Change a Table of Contents

When the text input position is in an existing table of contents, `TableOfContentsDialog` shows the attributes of that table of contents instead of inserting a new one. The user changes the structure levels, the title or the page numbers, and the method returns the identifier of the changed table of contents.

The **Edit** > **Table of Contents** > **Edit...** menu item of TXWords is the same call as the **Insert** menu item:

```vbscript
Private Sub mnuEdit_TOC_Edit_Click()
    TXTextControl1.TableOfContentsDialog
End Sub
```

### Update and Delete a Table of Contents

[TableOfContentsUpdate](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsupdate-method/llms-full.txt) brings its entries and page numbers up to date, for example after the user has added headings or the text has moved to other pages. [TableOfContentsDelete](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdelete-method/llms-full.txt) removes it from the document. Both take the identifier and return False when an error occurred or the identifier is invalid.

![The Table of Contents submenu of the TXWords Edit menu with Edit, Update and Delete.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/edit-toc-menu.c99e1031.webp)

TXWords calls them from **Edit** > **Table of Contents** > **Update** and **Delete**, with the identifier of the table of contents at the text input position:

```vbscript
Private Sub mnuEdit_TOC_Update_Click()
    TXTextControl1.TableOfContentsUpdate CurrentTOCId
End Sub

Private Sub mnuEdit_TOC_Delete_Click()
    TXTextControl1.TableOfContentsDelete CurrentTOCId
End Sub
```

To update a table of contents from code without the user being in it, keep the identifier that `TableOfContentsDialog` returned:

```vbscript
If Not TXTextControl1.TableOfContentsUpdate(TOCId) Then
   MsgBox "The table of contents could not be updated."
End If
```

### Key Members

| Member | Description |
|---|---|
| [TableOfContentsDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdialog-method/llms-full.txt) | Opens the built-in dialog box that inserts a table of contents or changes the one at the text input position, and returns its identifier. |
| [TableOfContentsUpdate](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsupdate-method/llms-full.txt) | Updates the entries and page numbers of a table of contents. |
| [TableOfContentsDelete](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdelete-method/llms-full.txt) | Deletes a table of contents. |
| [TableOfContentsEntered](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsentered-event/llms-full.txt), [TableOfContentsLeft](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsleft-event/llms-full.txt) | Occur when the text input position moves into or out of a table of contents. |
| [TableOfContentsCreated](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentscreated-event/llms-full.txt), [TableOfContentsDeleted](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/tableofcontentsdeleted-event/llms-full.txt) | Occur when a table of contents has been created or deleted. |
| [ParagraphDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/paragraphdialog-method/llms-full.txt) | Opens the paragraph dialog box, in which users set the structure level of paragraphs. |

The programming category [Using a Table of Contents](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/table-of-contents/llms-full.txt) lists all members for tables of contents.

### Related Pages

- [Stylesheets](https://docs.textcontrol.com/tx-text-control-activex/document-editor/stylesheets/llms-full.txt): paragraph styles that carry the structure level of headings.
- [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt): the paragraph dialog box and paragraph attributes.
- [Hypertext Links](https://docs.textcontrol.com/tx-text-control-activex/document-editor/fields/hyperlinks/llms-full.txt): internal links like the ones between entries and headings.
- [Sections and Page Setup](https://docs.textcontrol.com/tx-text-control-activex/document-editor/sections/llms-full.txt): the pages whose numbers the table of contents shows.
- [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/llms-full.txt): the TXWords sample program and its menus.
- [Using a Table of Contents](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/table-of-contents/llms-full.txt): all members for tables of contents.

---

## Nearby

- **Previous:** [Footnotes](https://docs.textcontrol.com/tx-text-control-activex/document-editor/footnotes/llms-full.txt)
- **Next:** [Form Fields](https://docs.textcontrol.com/tx-text-control-activex/document-editor/form-fields/llms-full.txt)
