Solved

Word Macro to Select Multiple Paragraphs Based On Content

Posted on 2013-07-01
3
1,081 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39291006
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
ID: 39291058
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
ID: 39292411
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

710 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