Word VBA: Only display outline level 1 text and TC field with outline level 2 in TOC

Viewed 20

I'm working with many similar documents given to me, which contains wdStyleHeading1 and 4-5 wdStyleHeading2 below each heading 1. Below each wdStyleHeading1 is a title which I would like to display in the TOC, so it looks like TOC example.

I add this title to the TOC as a TC field with ActiveDocument.TablesOfContents.MarkEntry Range:=Selection.Range, Entry:="TitleSample", Level:=1 for each wdStyleHEading1 which I encounter in my loop.

I create my TOC with the following code:

With ActiveDocument
      .TablesOfContents.Add Range:=Selection.Range, _
            RightAlignPageNumbers:=True, _
            UseHeadingStyles:=True, _
            UpperHeadingLevel:=1, _
            LowerHeadingLevel:=2, _
            UseFields:=True, _
            IncludePageNumbers:=True, _
            AddedStyles:="", _
            UseHyperlinks:=True, _
            HidePageNumbersInWeb:=True, _
            UseOutlineLevels:=True
      .TablesOfContents(1).TabLeader = wdTabLeaderDots
      .TablesOfContents.Format = wdIndexIndent
End With

However, I'm at a loss to how I can avoid displaying the 4-5 wdStyleHeading2 in the TOC, while still displaying the TC field titles I added with the MarkEntry method. I have attempted to use Paragraph.OutlineDemote method, but this fails since the document only contains wdOutlineLevel1 and wdOutlineLevel2.

Would the simplest fix be, to just create a style/formatting similar to wdStyleHeading2 which does not contain an outline level and apply this to all paragraphs which have the `wdStyleHeading2, or is there a way where I can keep the default style and omit it from the TOC?

And a bonus question: Is there a way to omit the TabLeader and page numbering for the outline level 1 entries and not outline 2 entries?

0 Answers
Related