Solved

Word Macro to Select Multiple Paragraphs Based On Content

Posted on 2013-07-01
3
936 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

910 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

18 Experts available now in Live!

Get 1:1 Help Now