• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 35
  • Last Modified:

Word VBA - Find is not finding Heading 3 (oddly)

On my Word document I have styles "Heading 1", "Heading 2" and "Heading 3". The following loop takes the Selection to the next "Heading 1" in turn, as it should. "Heading 2" works as well. But when I run "Heading 3" it DOES NOT go to the next "Heading 3", but selects the same (first) one each time round the loop.

Do
        With rngContent.Find
         .ClearFormatting
         .Style = "Heading 1"
         .Execute Forward:=True
        End With

            rngContent.Paragraphs(1).Range.Words(1).Select

Loop

Open in new window


Ideally I would attach the document to find out what is 'odd' about "Heading 3". The document is provided by my client, styles are applied already by them (this is a 500 page specification for engineering works). Unfortunately, for reasons of confidentiality I can't attach it here for you to look at.

But can anyone suggest what reasons could be causing this odd behaviour? What can I check?

FYI: rngContent is the entire document.
0
hindersaliva
Asked:
hindersaliva
  • 7
  • 5
3 Solutions
 
Martin LissOlder than dirtCommented:
When you manually select one of the other Heading 3's, what does Word say about its Style?
0
 
Martin LissOlder than dirtCommented:
Could extract a page or two and change the content including the text of the headings and post that?
0
 
hindersalivaAuthor Commented:
Martin, here's one page that has all 3 Heading styles.
M03 is a Heading 1
All the rest are Heading 2
Except M030704 is Heading 3 (but is behaving oddly)


In Sub FillArrayAll() you'll see a reduced version of the code I'm running, in order to get the Headings outline into an array.
Heading-3-issue-EE.docm
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Martin LissOlder than dirtCommented:
When do you mean when you say it's "behaving oddly". It's the only one so what can I compare it to?
0
 
hindersalivaAuthor Commented:
I mean the Find is 'treating' Heading 3 in a different way to Heading 1 and Heading 2.
eg.
Heading 1 - the Find goes from one Heading 1 to the next Heading 1.
Heading 2 is same
Heading 3 - the same Find code finds the 1st Heading 3, then finds the same one again, and repeats, stuck in a loop. Does not 'find' the next one.

In the sample attached, is Heading 3 different in some way to Heading 1 and 2?
0
 
Martin LissOlder than dirtCommented:
When I test your sample it does not get stuck in a loop; it finds the one Heading 3, processes it and then displays this
Cursor_and_Microsoft_Word.jpg
0
 
Martin LissOlder than dirtCommented:
Also, and I don't know if it makes a difference but when I click on the heading it shows this style which has some "Sub-clause..." wording following "Heading 3".
Cursor_and_Heading-3-issue-EE_docm_-.jpgDoes the "Contents Heading" part mean that it's a Table of Contents heading?
0
 
Martin LissOlder than dirtCommented:
I may know what the problem is. Try changing line 29 as shown because in line 66 your are comparing using a Trim'ed value and if there's a space or some unprintable character there won;t be a match with the un-trimmed value.
Sub FillArrayAll()
    
    'Find Headings code reduced in order to troubleshoot the 'Heading 3 issue'. 14/02/2018
    'Description of Issue:
    'Find is finding Heading 2 and 2 correctly, but not Heading 3
    
    
    dteStart = Now
    
    Application.ScreenUpdating = False
    
    Dim rngContent As Range
    Dim docThis As Document
    
    ReDim arrItemsAll(1 To 6, 1 To 2000)
    
    Set docThis = ActiveDocument

    Set rngContent = docThis.Bookmarks("TheContent").Range     'This is to make the Find start from the top

    strHeadingToFind = "Heading 3"  '<<<<<<<<<<<<<< EDIT THIS TO test with Heading 2 and 3

    'find last item
    With rngContent.Find
     .ClearFormatting
     .Style = strHeadingToFind
     .Execute Forward:=False
    End With
    strLastItemID = Trim(rngContent.Paragraphs(1).Range.Words(1).Text)

    Set rngContent = docThis.Bookmarks("TheContent").Range     'to make the Find start from the top

    i = 1

    ActiveDocument.ActiveWindow.View.ShowHiddenText = True

    Do
        With rngContent.Find
         .ClearFormatting
         .Style = strHeadingToFind
         .Execute Forward:=True
        End With

        If Left(rngContent.Paragraphs(1).Range.Words(1).Text, 1) = "M" Then     'to exclude rogue Headings


            rngContent.Paragraphs(1).Range.Words(1).Select

            arrItemsAll(1, i) = rngContent.Paragraphs(1).Range.Words(1).Text
            arrItemsAll(2, i) = Right(rngContent.Paragraphs(1).Range.Text, Len(rngContent.Paragraphs(1).Range.Text) - 8)
            arrItemsAll(3, i) = i
            arrItemsAll(4, i) = rngContent.Paragraphs(1).Range.Start

            If rngContent.Paragraphs(1).Range.Font.Hidden Then
                arrItemsAll(5, i) = "False"
            End If
            If Not rngContent.Paragraphs(1).Range.Font.Hidden Then
                arrItemsAll(5, i) = "True"
            End If
            arrItemsAll(6, i) = ""

        End If

        i = i + 1

        If Trim(rngContent.Paragraphs(1).Range.Words(1).Text) = strLastItemID Then Exit Do

    Loop

    ActiveDocument.ActiveWindow.View.ShowHiddenText = False

    'add an END item to the array
    arrItemsAll(1, i) = "End"
    arrItemsAll(2, i) = "End"
    arrItemsAll(3, i) = i
    arrItemsAll(4, i) = ActiveDocument.Bookmarks("TheContent").End
    arrItemsAll(5, i) = ""
    arrItemsAll(6, i) = "End SubClauses"

    dteEnd = Now

    intItemsAll = i

    ReDim Preserve arrItemsAll(1 To 6, 1 To intItemsAll)
    
    MsgBox intItemsAll & vbCr & dteStart & vbCr & dteEnd
    
End Sub

Open in new window

0
 
hindersalivaAuthor Commented:
Martin, thanks for helping me.
I now see it. There's a table below the M030701 section. Wherever there's a table the Find doesn't proceed any further. I don't see why that is (?). The marked ones are Heading 3. And I have selected the table so you can see it.
Original document sampleThe reason the problem is only with Heading 3 is (looks like) only Heading 3 sections have tables in this doc.

So, my quest has moved on. How can I make the Find carry on past the tables? Hmmmm. I'll post another question when I find out more about these tables.
0
 
Martin LissOlder than dirtCommented:
OK, but I think you should make the change I suggested anyhow,
0
 
hindersalivaAuthor Commented:
Yes, I agree Martin.
0
 
hindersalivaAuthor Commented:
I was able to do some more tests on the original document and saw why it was behaving the way it was.
Bottom line: Find gets interrupted when it encounters one the tables, or tables generally. Don't know why though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now