Link to home
Create AccountLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Word VBA - looping and Paragraphs

I'm finding that looping through 5,000+ paragraphs in a document, to find approx 40 that have a Heading 1 style, is slow (takes minutes, and not milliseconds).

Is there a quick way of 'finding the next Heading 1 style'?

I heard that with the Paragraphs collection we don't need to loop through to work with the paragraphs. I don't know if that helps with my quest.

Thanks for any help.
Avatar of hindersaliva
hindersaliva
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Ah sorry. Found the answer. There appears to be a Find.Style = Heading 1"

I'll leave the question open as there may be useful contributions to the topic from others.
ASKER CERTIFIED SOLUTION
Avatar of crystal (strive4peace)
crystal (strive4peace)

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of crystal (strive4peace)
crystal (strive4peace)

Eric, thanks for commenting. I see your point.  The reason I like to use one Heading 1 tag and the rest Heading 2 and below is:

The TOC picks up levels of Heading tags, as do other built-in features
and
on a web page, for SEO*, if there is more than one H1 (Heading 1) tag, or it is missing, it does not get read for H1

*Search Engine Optimization. While this is a Word doc, not a web page, whenever possible it is good to keep the same rules -- besides, it might become a web page someday ... or get searched like one.

I've been using the Heading styles built into Word for a few decades. It works great and is, in my opinion, a good habit.

If you are writing a book and have lots of chapters, it is good to use Master Document and Subdocuments with each chapter in a separate file -- in which case, each chapter may have one heading 2, and no heading 1 since the master document would have heading 1.

have an awesome day,
crystal
Eric, yes I have to use VBA. It's to automate the authoring of 400+ page specifications with 900+ Headings 1, 2 and 3. The procedures will Hide/Show the required parts selectable from cascading UserForms, which themselves are built 'on  the fly' from the parts that exist on the document. So, yes -.not a manual authoring scenario.

Crystal's code has got me most of the way. See earlier questions.

BTW I'm using Find, rather than looping over the paragraphs. It's a lot faster.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Experts,
I have one further question re. Find.
Find works great, much quicker than looping through all the paragraphs. But I have an issue. This is the code I use to find only Heading 1.

    With rngContent.Find
     .ClearFormatting           'why is this here? what does it do?
     .Style = "Heading 1,Section Heading,Head 1,VS1"
     .Execute Forward:=False
    End With

Open in new window


But, some of the items will be Hidden. The above Find doesn't find those. So I tried ...
.Font.Hidden = True

Open in new window


But that would only find those that are Hidden.

I need to Find both Hidden and Not Hidden, in the order in the document so it needs to be a 'top-to-bottom' run (not two runs). And grab the Hidden state while I'm at it, of course. (I stuff the data into an array during the run BTW)

I'd be grateful for ideas. Thanks.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Eric, that's brilliant!