Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

When you manually select one of the other Heading 3's, what does Word say about its Style?
Could extract a page or two and change the content including the text of the headings and post that?
Avatar of hindersaliva

ASKER

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
When do you mean when you say it's "behaving oddly". It's the only one so what can I compare it to?
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?
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
User generated image
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, but I think you should make the change I suggested anyhow,
Yes, I agree Martin.
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.