Loop through all the paragraphs of the main document sections

Posted on 2009-02-17
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
    LVL 23

    Accepted Solution

    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

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

    Author Comment

    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

    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

    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

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
    I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now