Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word Macro to Select Multiple Paragraphs Based On Content

Posted on 2013-07-01
3
Medium Priority
?
1,209 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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…

661 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