none
How to use VBA in MS word to find out how many headings in a doc? RRS feed

  • Question

  • Here is my code:

    Dim rng As Range
        With ActiveDocument
        For i = 1 To 20 Step 1
            Set rng = .GoTo(what:=wdGoToHeading)
            'Selection.GoTo what:=wdGoToHeading, which:=GoToNext
            Set rng = rng.GoTo(what:=wdGoToBookmark, Name:="\line")
            MsgBox (rng.Text)
            Next i
     End With

    1)The cursor will just stay at the last headings if i still haven't reached 20. But I want to exit the for loop if I reached the last headings. Anyone has ideas?

    2) Is it a way to present the name as well as the content of a heading? For example, when we are writing a paper we have abstract section before body text, is it a way to print the content of abstract on a message box?  

    3) Suppose I have two headings A and B. In MS word B is the subsection of A. In VBA, Is it a way to find out if heading B is the subheading of A?

    Thank you so much, everyone!

    Thursday, April 2, 2015 6:34 PM

Answers

  • Hi

    >>How to use VBA in MS word to find out how many headings in a doc?

    The following code you can refer to find out all the headings in a doc, which will list all the headings and the amount of the headings.

    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

     

     

        Set rng = docOutline.Content

        astrHeadings = _

         docSource.GetCrossReferenceItems(wdRefTypeHeading)

        Dim count As Integer

        For intItem = LBound(astrHeadings) To UBound(astrHeadings)

            ' Get the text and the level.

            strText = Trim$(astrHeadings(intItem))

            intLevel = GetLevel(CStr(astrHeadings(intItem)))

            count = count + 1

            rng.InsertAfter strText & vbNewLine

            rng.Style = "Heading " & intLevel

            rng.Collapse wdCollapseEnd

        Next intItem

        rng.InsertAfter "Total Headings : " & count

       

    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

    >> 1)The cursor will just stay at the last headings if i still haven't reached 20. But I want to exit the for loop if I reached the last headings. Anyone has ideas?

    I have followed these code you have provided , the cursor will stay where it used to be, and it will not goto  next bookmark because there is no "which " for the sentence to goto next one,so it will goto the last one. and if you want to break out form a for loop ,you can use "exit for " to exit a loop.

    By the way, you have more than one question ,better to post these issues separately , for more community will be involved in, you can get more qualified pool of respondents.

    Thanks for your understanding.

    Best Regards

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by L.HlModerator Tuesday, April 14, 2015 2:37 PM
    • Marked as answer by L.HlModerator Monday, April 20, 2015 10:12 AM
    Friday, April 3, 2015 10:03 AM
    Moderator

All replies

  • Hi

    >>How to use VBA in MS word to find out how many headings in a doc?

    The following code you can refer to find out all the headings in a doc, which will list all the headings and the amount of the headings.

    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

     

     

        Set rng = docOutline.Content

        astrHeadings = _

         docSource.GetCrossReferenceItems(wdRefTypeHeading)

        Dim count As Integer

        For intItem = LBound(astrHeadings) To UBound(astrHeadings)

            ' Get the text and the level.

            strText = Trim$(astrHeadings(intItem))

            intLevel = GetLevel(CStr(astrHeadings(intItem)))

            count = count + 1

            rng.InsertAfter strText & vbNewLine

            rng.Style = "Heading " & intLevel

            rng.Collapse wdCollapseEnd

        Next intItem

        rng.InsertAfter "Total Headings : " & count

       

    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

    >> 1)The cursor will just stay at the last headings if i still haven't reached 20. But I want to exit the for loop if I reached the last headings. Anyone has ideas?

    I have followed these code you have provided , the cursor will stay where it used to be, and it will not goto  next bookmark because there is no "which " for the sentence to goto next one,so it will goto the last one. and if you want to break out form a for loop ,you can use "exit for " to exit a loop.

    By the way, you have more than one question ,better to post these issues separately , for more community will be involved in, you can get more qualified pool of respondents.

    Thanks for your understanding.

    Best Regards

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by L.HlModerator Tuesday, April 14, 2015 2:37 PM
    • Marked as answer by L.HlModerator Monday, April 20, 2015 10:12 AM
    Friday, April 3, 2015 10:03 AM
    Moderator
  • But how to know if that is the very last headings? Without that, I am not able to break the for loop
    Wednesday, April 8, 2015 3:41 AM
  • Hi,

    The code above could get the amount of the headings. If you want to loop the headings .you can do it like this.

    For i=1 to amount
    
    …….. ' put your code here
    
    Next i
    

    There is no need to break the loop. When I equals to amount, it will naturally end the loop.

    or

    For i = 1 To 20 Step 1
    ' put your code here
    If i = amount Then Exit For 'amount should less than 20
    Next i
    End Sub

    Best Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 9, 2015 10:20 AM
    Moderator
  • So my next question is, how to get the paragraph inside the heading.

    For example, if I have two paragraph (see below) belongs to Heading 1, how should I retrieve that (or show them inside a MsgBox)?

    Heading1

    Paragraph1

    Paragraph2

    Heading2

    Paragraph3

    Paragraph4

    Thanks

    Wednesday, April 15, 2015 8:48 AM
  • Hi,

    Since your orginal question has solved, I recommend you post a new thread about this question, how to get content of the heading in a world, for you will get the most qualified pool of respondents.

    Thanks for your understanding.

    Best Regards

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 16, 2015 10:05 AM
    Moderator