I'm trying to write code to insert a custom table of contents. The code first writes "Table of Contents" in a new paragraph and sets that paragraph to a style
other than Heading 1 or Heading 2 (though it is based on Heading 1).
Then I try this:
Code:
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
:="Heading 1,Heading 2", UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, UseOutlineLevels:=False
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With
Despite code specifying exactly what styles to use, the resulting TOC uses Doc Title, all nine Heading N styles, and the custom TOC style -- which results in the TOC displaying an entry for itself, which is just stupid.
What am I missing?
(FWIW, if I turn on the macro recorder and insert a TOC that correctly excludes the TOC style, I get exactly what I want, and I get almost exactly the code above. The only difference is that the recorder writes
Code:
AddedStyles:="Heading 1, 1, Heading 2, 2"
. The MSDN reference for the TableOfContents.Add method does not show the levels, just a comma-delimited list of styles to include. Doesn't matter; the code fails the same way.)