Solved

Word Macro to Select Multiple Paragraphs Based On Content

Posted on 2013-07-01
3
896 Views
Last Modified: 2013-07-05
I have a document containing multiple short paragraphs.  The paragraphs are written in groups where the first starts with "Software Specification".  There are three more paragraphs immediately following and then a Fifth that starts with "Area Path".

I need a macro that will look through the entire document, adjusted the selected text to be a block of the five paragraphs, put borders around all, then move on to the next block.  I know how to do the borders once the block is selected but not how to cycle through the blocks.

Once that's completed, I want to cycle through, paragraph by paragraph, and if it begins with "Software Specification" set the shading.  Again, I can do the shading on a selection but don't know how to cycle through each paragraph looking for the correct one.
0
Comment
Question by:rmbrown
  • 2
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
You can select the text using a wildcard find, e.g.:
Sub FindParas()
    Dim rng As Range
    Dim brd As Border
    
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "Software Specification*^13Area Path*^13"
        .MatchWildcards = True
        Do While .Execute
            'your code here, e.g.
            For Each brd In rng.Paragraphs.Borders
                With brd
                    .LineStyle = wdLineStyleDashDotStroked
                End With
            Next brd
            
            rng.Collapse wdCollapseEnd
            rng.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window

0
 

Author Comment

by:rmbrown
Comment Utility
Here's what I tried...  Only the first paragraph was selected and bordered.  Thoughts?

Sub PutBordersAroundSelection()
    Dim rng As Range
    Dim brd As Border
   
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "Software Specification*^13Area Path*^13"
        .MatchWildcards = True
        Do While .Execute
            With Selection.Borders(wdBorderTop)
                .LineStyle = Options.DefaultBorderLineStyle
                .LineWidth = Options.DefaultBorderLineWidth
                .Color = Options.DefaultBorderColor
            End With
            With Selection.Borders(wdBorderLeft)
                .LineStyle = Options.DefaultBorderLineStyle
                .LineWidth = Options.DefaultBorderLineWidth
                .Color = Options.DefaultBorderColor
            End With
            With Selection.Borders(wdBorderBottom)
                .LineStyle = Options.DefaultBorderLineStyle
                .LineWidth = Options.DefaultBorderLineWidth
                .Color = Options.DefaultBorderColor
            End With
            With Selection.Borders(wdBorderRight)
                .LineStyle = Options.DefaultBorderLineStyle
                .LineWidth = Options.DefaultBorderLineWidth
                .Color = Options.DefaultBorderColor
            End With
            With Selection.Borders(wdBorderHorizontal)
                .LineStyle = Options.DefaultBorderLineStyle
                .LineWidth = Options.DefaultBorderLineWidth
                .Color = Options.DefaultBorderColor
            End With
           
            rng.Collapse wdCollapseEnd
            rng.End = ActiveDocument.Range.End
        Loop
    End With

End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
Obviously you have plugged a recorded macro into the loop. Of necessity, because such a macro records the user's actions it uses the Selection object. It also fills in all the default settings, so that the code is much more bulky.

I always recommend, where possible, using a Range object instead of the Selection object. It is generally faster with less screen flicker. Also you can have an unlimited number of range objects, while there is only one Selection object per application.

However, the code is now mixed. The Find changes the Range object (rng), but not the Selection.

Here is my macro modified to do what I think you are trying to do:
Sub FindParasRange()
    Dim rng As Range
    Dim brd As Border
    
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "Software Specification*^13Area Path*^13"
        .MatchWildcards = True
        Do While .Execute
            'your code here, e.g.
            For Each brd In rng.Paragraphs.Borders
                With brd
                    .LineStyle = Options.DefaultBorderLineStyle
                End With
            Next brd
       Loop
    End With
End Sub

Open in new window

Here is the same code, using the Selection object:
Sub FindParasSelection()
    Dim brd As Border
    
    ActiveDocument.Range.Select
    With Selection.Find
        .Text = "Software Specification*^13Area Path*^13"
        .MatchWildcards = True
        Do While .Execute
            'your code here
            For Each brd In Selection.Paragraphs.Borders
                With brd
                    .LineStyle = Options.DefaultBorderLineStyle
                End With
            Next brd
        Loop
    End With
End Sub

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

762 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

12 Experts available now in Live!

Get 1:1 Help Now