none
How can I get a list of a document's headings without having to manually scan through the entire document? RRS feed

  • Question

  • I'm trying to get a list of all headings in a page so I can validate that they're structured correctly for a program we're working on.

    Currently the only way I know how to get the headings is to convert the whole document to HTML, scan through it line by line, add any line with a heading tag to an array, use some regex to extract the text and heading level, then put it all into an array.

    This works well enough on most documents, but when it runs into a larger document it can take minutes to complete. Word must already have an easier way to do this built-in, because I can open the Document Map in the Navigation Pane, and it'll load and display all of my headings almost instantaneously.

    Is there a way for me to access the data the Document Map uses to do this?
    • Edited by Thorium92 Monday, April 8, 2019 5:14 AM
    Monday, April 8, 2019 5:14 AM

Answers

  • Try:

    Sub InsertTOC()
    With ActiveDocument
      .Range.InsertAfter vbCr
      .Fields.Add .Characters.Last, wdFieldEmpty, "TOC", False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Thorium92 Wednesday, April 10, 2019 1:20 AM
    Tuesday, April 9, 2019 11:43 AM

All replies

  • Would Outline View do what you want? If you show Level 9, you'll only see the headings, and you'll be able to edit them.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, April 8, 2019 9:40 AM
  • Or a macro:

    Sub ListHeadings()
        Dim docS As Document
        Dim docT As Document
        Dim arrHeadings() As String
        Dim hdg As Variant
        Application.ScreenUpdating = False
        Set docS = ActiveDocument
        arrHeadings = docS.GetCrossReferenceItems(wdRefTypeHeading)
        Set docT = Documents.Add
        For Each hdg In arrHeadings
            docT.Content.InsertAfter hdg & vbCr
        Next hdg
        Application.ScreenUpdating = True
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, April 8, 2019 9:55 AM
  • You could just temporarily insert a Table of Contents at the end of the document to get both the headings and the pages they're on.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 8, 2019 9:15 PM
  • Thanks for the suggestion, that might work :)
    Is there a way to insert a table of contents programmatically? Searching for 'contents' in the API documentation doesn't bring up anything useful
    Tuesday, April 9, 2019 2:14 AM
  • Here is a macro that inserts a Table of Contents at the end of the document:

     Sub CreateTOC()
        Selection.EndKey Unit:=wdStory
        Selection.InsertParagraphAfter
        ActiveDocument.Fields.Add _
            Range:=Selection.Range, _
            Type:=wdFieldTOC, _
            Text:="\o ""1-9"" \h \z \u"
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Tuesday, April 9, 2019 9:43 AM
  • Try:

    Sub InsertTOC()
    With ActiveDocument
      .Range.InsertAfter vbCr
      .Fields.Add .Characters.Last, wdFieldEmpty, "TOC", False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Thorium92 Wednesday, April 10, 2019 1:20 AM
    Tuesday, April 9, 2019 11:43 AM
  • Thanks for the help!
    Wednesday, April 10, 2019 1:19 AM