Word VBA - Go to next Heading problem

In Word VBA, I'm trying to find a faster way of finding Headings than Find. I tried this but it finds the same 1st Heading each time round the loop. I'm obviously missing something easy. Thanks for help.
Sub FindHeadingsInSequence()

    Dim rngContent As Range
    
    Set rngContent = ThisDocument.Bookmarks("TheContent").Range
    
    Do
        rngContent.GoTo what:=wdGoToHeading, Which:=wdGoToNext, Count:=1, Name:=""
    
        MsgBox rngContent.Paragraphs(1).Range.Words(1).Text
    
    Loop

End Sub

Open in new window

hindersalivaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Built in functions like Find will always be faster than anything that can be written in code.
Rgonzo1971Commented:
Hi,

Even if its not the quickest
Sub FindHeadingsInSequence()

    Dim rngContent As Range
    
    Set rngContent = ThisDocument.Bookmarks("TheContent").Range
    Set Heading = rngContent
    Do   ' Loop through headings
    Dim current As Long
    current = Heading.Start
    ' doest not pick 1st Heading at begin of text
    Set Heading = Heading.GoTo(What:=wdGoToHeading, Which:=wdGoToNext) 
    If Heading.Start = current Then
        Exit Do
    End If
    MsgBox Heading.Paragraphs(1).Range.Words(1).Text
Loop
End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Sub FindHeadingsInSequence()

    Dim rngContent As Range
    Dim intCount As Integer
    
    Set rngContent = ThisDocument.Bookmarks("TheContent").Range
    
    Do
        intCount = intCount + 1
        If intCount > rngContent.Paragraphs.Count Then
            Exit Sub
        End If
        rngContent.GoTo what:=wdGoToHeading, Which:=wdGoToNext, Count:=intCount, Name:=""
        
        ' Word seems to consider a lot of things headings and so
        ' this 'If' filters them
        If Left(rngContent.Paragraphs(intCount).Style.NameLocal, 7) = "Heading" Then
            ' And you seem to have a number for words that use a "heading" style
            ' that don't look like headings, so this only shows 'Heading 1' to 4
            If Mid$(rngContent.Paragraphs(intCount).Style.NameLocal, 9, 1) < 5 Then
                MsgBox rngContent.Paragraphs(intCount).Range.Words(1).Text
            End If
        End If
    Loop

End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.