VBA Code
VBA 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)
VBA - Table of Figures
You can have more than one table of figures in a document since it is a collection object.
TablesOfFigures object
ActiveDocument.TablesOfFigures.Add Range:=Selection.Range, _
Caption:=wdCaptionLabelID.wdCaptionFigure, _
IncludeLabel:=True, _
UpperHeadingLevel:=1, _
LowerHeadingLevel:=9, _
UseFields:=False, _
TableID:="", _
RightAlignPageNumbers:=True, _
IncludePageNumbers:=True, _
AddedStyles:="", _
UseHyperlinks:=False, _
HidePageNumbersInWeb:=True
Range - Returns a Range object that represents the portion of a document that is contained within the specified table of contents.
Caption -
IncludeLabel - (Boolean) Whether to include the caption label and caption number in the table of figures.
UpperHeadingLevel - (Long) Returns or sets the starting heading level for a table of contents.
LowerHeadingLevel - (Long) Returns or sets the ending heading level for a table of contents or table of figures.
UseFields - (Boolean) Whether to use Table of Contents Entry (TC) fields to create the table of figures.
TableID - (String) Returns or sets a one-letter identifier that's used to build a table of contents from TOC fields.
RightAlignPageNumbers - (Boolean) Whether page numbers are aligned with the right margin in the table of figures.
IncludePageNumbers - (Boolean) Whether to include page numbers in the table of contents. Read/write Boolean.
AddedStyles -
UseHyperlinks - (Boolean) Returns or sets whether entries in a table of contents should be formatted as hyperlinks when publishing to the Web.
HidePageNumbersInWeb - (Boolean) Returns or sets whether page numbers in a table of contents or a table of figures should be hidden when publishing to the Web.
List of Tables
wdCaptionLabelID.wdCaptionTable
VBA - Table of Authorities
VBA - Cross Referencing
InsertCrossReference
Inserts a cross-reference to a heading, numbered item, bookmark, footnote, or endnote
Can also be used to insert a cross-reference to an item that has a caption label (for example, an equation, figure, or table).
Selection.InsertCrossReference ReferenceType:=WdReferenceType.wdRefTypeNumberedItem, _
ReferenceKind:=WdReferenceKind.wdNumberNoContext, _
ReferenceItem:=1, _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "
ReferenceType - The type of item for which a cross-reference is to be inserted.
Can be any wdReferenceType or wdCaptionLabelID constant or a user defined caption label.
ReferenceKind - The information to be included in the cross-reference.
If you specify wdPageNumber for the value of ReferenceKind, you may need to repaginate the document to see the correct cross-reference information.
ReferenceItem - If ReferenceType is wdRefTypeBookmark, this argument specifies a bookmark name.
For all other ReferenceType values, this argument specifies the item number or name in the Reference type box in the Cross-reference dialog box.
Use the GetCrossReferenceItems method to return a list of item names that can be used with this argument.
InsertAsHyperlink - (Optional) default is True to insert the cross-reference as a hyperlink.
IncludePosition - (Optional) default is True to insert "above" or "below," depending on the location of the reference item in relation to the cross-reference.
SeparateNumbers - (Optional) True to use a separator to separate the numbers from the associated text.
(Use only if the ReferenceType parameter is set to wdRefTypeNumberedItem and the ReferenceKind parameter is set to wdNumberFullContext.)
SeparatorString - (Optional) Specifies the string to use as a separator if the SeparateNumbers parameter is set to True.
Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=WdReferenceKind.wdNumberRelativeContext, _
ReferenceItem:="2", _
InsertAsHyperlink:=True,
IncludePosition:=False,
SeparateNumbers:=False, _
SeparatorString:=" "
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=WdReferenceKind.wdContentText, _
ReferenceItem:="2", _
InsertAsHyperlink:=True,
IncludePosition:=False,
SeparateNumbers:=False, _
SeparatorString:=" "
With Selection
.Collapse Direction:=wdCollapseStart
.InsertBefore "For more information, see "
.Collapse Direction:=wdCollapseEnd
.InsertCrossReference ReferenceType:=WdReferenceType.wdRefTypeHeading, _
ReferenceKind:=WdReferenceKind.wdContentText, _
ReferenceItem:=1
.InsertAfter " on page "
.Collapse Direction:=wdCollapseEnd
.InsertCrossReference ReferenceType:=WdReferenceType.wdRefTypeHeading, _
ReferenceKind:=WdReferenceKind.wdPageNumber, _
ReferenceItem:=1
.InsertAfter "."
End With
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev