Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 71459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:53:28+00:00 2026-05-10T19:53:28+00:00

How do I get a list of all the headings in a word document

  • 0

How do I get a list of all the headings in a word document by using VBA?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:53:28+00:00Added an answer on May 10, 2026 at 7:53 pm

    You mean like this createOutline function (which actually copy all headings from a source word document into a new word document):

    (I believe the astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading) function is the key in this program, and should allow you to retrieve what you are asking for)

    Public Sub CreateOutline()     Dim docOutline As Word.Document     Dim docSource As Word.Document     Dim rng As Word.Range          Dim astrHeadings As Variant     Dim strText As String     Dim intLevel As Integer     Dim intItem As Integer              Set docSource = ActiveDocument     Set docOutline = Documents.Add          ' Content returns only the main body of the document, not the headers/footer.             Set rng = docOutline.Content     ' GetCrossReferenceItems(wdRefTypeHeading) returns an array with references to all headings in the document     astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading)          For intItem = LBound(astrHeadings) To UBound(astrHeadings)         ' Get the text and the level.         strText = Trim$(astrHeadings(intItem))         intLevel = GetLevel(CStr(astrHeadings(intItem)))                  ' Add the text to the document.         rng.InsertAfter strText & vbNewLine                  ' Set the style of the selected range and         ' then collapse the range for the next entry.         rng.Style = "Heading " & intLevel         rng.Collapse wdCollapseEnd     Next intItem End Sub  Private Function GetLevel(strItem As String) As Integer     ' Return the heading level of a header from the     ' array returned by Word.          ' The number of leading spaces indicates the     ' outline level (2 spaces per level: H1 has     ' 0 spaces, H2 has 2 spaces, H3 has 4 spaces.              Dim strTemp As String     Dim strOriginal As String     Dim intDiff As Integer          ' Get rid of all trailing spaces.     strOriginal = RTrim$(strItem)          ' Trim leading spaces, and then compare with     ' the original.     strTemp = LTrim$(strOriginal)          ' Subtract to find the number of     ' leading spaces in the original string.     intDiff = Len(strOriginal) - Len(strTemp)     GetLevel = (intDiff / 2) + 1 End Function 

    UPDATE by @kol on March 6, 2018

    Although astrHeadings is an array (IsArray returns True, and TypeName returns String()) I get a type mismatch error when I try to access its elements in VBScript (v5.8.16384 on Windows 10 Pro 1709 16299.248). This must be a VBScript-specific problem, because I can access the elements if I run the same code in Word’s VBA editor. I ended up iterating the lines of the TOC, because it works even from VBScript:

    For Each Paragraph In Doc.TablesOfContents(1).Range.Paragraphs   WScript.Echo Paragraph.Range.Text Next 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get list of all existing accounts from Outlook 2003 using
Possible Duplicate: Get list of all input objects using JavaScript, without accessing a form
I am using webbrowser control , and i get list of all the url
How can I get list of all tables in SQL Server database using query.
i want to get list of all application or Threads attached with a process.For
How can I to get list of all wcf services running on a machine?
Trying to get a list of all substrings currently I have a function but
I want to get a list of all TRs that do not include THs.
I want to get the list of all the bills for which a particular
I need a get a list of all databases in a Couchdb server inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.