Loop through all the paragraphs of the main document sections

Posted on 2009-02-17
Medium Priority
Last Modified: 2012-05-06
Dear Experts:

I got a document which loops thru all the paragraphs of my document and does stuff to all the paragraphs that meet a certain criterion.

The document in question is a multi-section document.
I wonder whether it pssible to have Word loop only thru those paragraphs that reside in  sections that contain built-in headings level 1.

Help is appreciated. Thank you very much in advance. Regards, Andreas
Question by:AndreasHermle
  • 3
  • 2
LVL 23

Accepted Solution

irudyk earned 2000 total points
ID: 23671207
You could try to adopt the following within the code you are currently using:
Sub ParseHeading1Sections()
Dim p As Paragraph
Dim blnInSection1 As Boolean
blnInSection1 = False
For Each p In ActiveDocument.StoryRanges(wdMainTextStory).Paragraphs
    If InStr(1, p.Range.Style, "heading ", vbTextCompare) <> 0 Then blnInSection1 = (p.Range.Style = "Heading 1")
    If blnInSection1 Then
        'your existing code here
    End If
End Sub

Open in new window

LVL 23

Expert Comment

ID: 23692070
Just curious if you had any luck with the above code?

Author Comment

ID: 23698774
Hey irudyk,

sorry for the delay in answering. I am afraid to say that the code is not working. The actual macro still loops thru paragraphs of sections that do not contain built-in headings level 1. The make-up of the documents in questions is allways the same.

Section 1: Cover Sheet
Section 2: Table of Contents (user-defined heading style)
Section 3: Table of Figures (user defined heading style)
Section x: List of Tables (user defined heading style)
Section x: Main document starting with built-in heading 1
Main document contains any number of sections, all starting with built-in heading 1

Section x: Bibliography (starting with a heading formatted with a user defined heading style called 'Bibliography Heading')

Now to be more exact:
The macro should create a range (sort of) that spans ...
... from the first occurrence of built-in heading 1 to the last word of the section that contains a heading formatted with the built-in heading 1 style.

Did this make it any clearer?Or maybe it is not feasible what I want.

Regards, Andreas
LVL 23

Expert Comment

ID: 23699966
Hmm, well I'm not sure why it would not work given the structure you've outlined.
The code will go through each paragraph in the main document until it finds one with Heading style 1.  If one is found, a variable is flagged to indicate that the code is in an area related to Heading 1 and parses the subsequent paragraphs as such unless it crosses a paragraph with the style starting with the name "Heading ".  If it does then it checks to see if it is Heading 1.  If not the variable is flagged as False and that paragraph and those subsequent are not processed unless a Heading 1 style is encountered again, and so on.
Now I don't have an elaborate document to test on (the one I tested on seemed to work), but I would suggest that you step through the code to see where it is failing in order to shed more light on the problem.

Author Comment

ID: 23706888
Hey irudyk,

my fault!  It is working as desired. I made a mistake on integrating my code into yours. Now I fixed it. It is working just fine. Thank you very much for your professional help. Regards, Andreas

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question