Link to home
Start Free TrialLog in
Avatar of rmbrown
rmbrown

asked on

Word Macro to Select Multiple Paragraphs Based On Content

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.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of rmbrown
rmbrown

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial