Share via

Select all paragraphs containing a given word or symbol [paragraph gathering]

Anonymous
2015-12-05T17:40:20+00:00

Is it possible to select all paragraphs containing a word or a symbol, so that they can be copied and pasted in the same consecutive order as they appear in the original text?

If not, a macro could do this perhaps?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
2015-12-06T07:27:43+00:00

While Jay is sleeping...

Assuming that it is headings with the style of Heading 1 that you are interested in, then the following should do what you want:

Dim sourceDoc As Document

Dim destDoc As Document

Dim srchRg As Range

Dim copyRg As Range

Dim searchTerm As String

Dim count As Long

Dim i As Long

Dim rngfound As Range

searchTerm = InputBox("Enter word or phrase to find:", "Search Term")

If searchTerm = "" Then Exit Sub

Set sourceDoc = ActiveDocument

Set destDoc = Documents.Add

Set srchRg = sourceDoc.Range

count = 0

With srchRg.Find

    .Text = searchTerm

    While .Execute

        count = count + 1

        Set copyRg = destDoc.Range

        copyRg.Collapse wdCollapseEnd

        If srchRg.Style = "Heading 1" Then

            Set rngfound = srchRg.Paragraphs(1).Range

            rngfound.End = sourceDoc.Range.End

            With rngfound

                For i = 2 To .Paragraphs.count

                    If .Paragraphs(i).Style = "Heading 1" Or InStr(.Paragraphs(i).Range, searchTerm) > 0 Then

                        rngfound.End = rngfound.Paragraphs(i - 1).Range.End

                        Exit For

                    End If

                Next i

            End With

        End If

        copyRg.FormattedText = rngfound.FormattedText

    Wend

End With

If count = 0 Then

    destDoc.Range.Text = "No instances found"

End If

Was this answer helpful?

0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2015-12-05T19:26:59+00:00

    No, Word can't do that directly.

    Without a macro, you can highlight just the search term wherever it appears, by using the search box in the Navigation pane and viewing the Results tab there. Then you would have to click each result, select its paragraph, and copy/paste it.

    With a macro, you can easily accomplish the job -- but not by selecting all the occurrences at once, because Word macros cannot make discontiguous selections in a document. Instead, the macro can copy the paragraph as each occurrence is found in a loop.

    The following is a bare-bones macro that illustrates the technique. With more code, the search can be made to offer all the options that are available in the Advanced Find dialog.

    Sub CopyFoundParagraphs()

        Dim sourceDoc As Document

        Dim destDoc As Document

        Dim srchRg As Range

        Dim copyRg As Range

        Dim searchTerm As String

        Dim count As Long

        searchTerm = InputBox("Enter word or phrase to find:", "Search Term")

        If searchTerm = "" Then Exit Sub

        Set sourceDoc = ActiveDocument

        Set destDoc = Documents.Add

        Set srchRg = sourceDoc.Range

        count = 0

        With srchRg.Find

            .Text = searchTerm

            While .Execute

                count = count + 1

                Set copyRg = destDoc.Range

                copyRg.Collapse wdCollapseEnd

                copyRg.FormattedText = srchRg.Paragraphs(1).Range.FormattedText

            Wend

        End With

        If count = 0 Then

            destDoc.Range.Text = "No instances found"

        End If

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-12-06T12:19:03+00:00

    Thanks much for the macro, Doug.

    When I run it I get an error message about variable not set at the line

    copyRg.FormattedText = rngfound.FormattedText

    I wasn't using Headings 1...

    So it works as intended and this is very useful.

    Was this answer helpful?

    0 comments No comments
  3. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2015-12-06T04:28:11+00:00

    I don't understand how the hundreds of repetitions can happen -- there must be something about either the text in your document or the text you told the macro to search for that I'm not accounting for. Could you upload your test file to a sharing site such as Dropbox or OneDrive, and post a link here for downloading it?

    I gather that when you say "headings and their contents" you mean a heading and all the text below it, down to but not including the next heading at the same level. I haven't looked at that situation before, so I'm not sure how much work it will be. It sounds like it should be handled by Outline view, but a quick test shows that it doesn't quite work that way. I'll experiment over the next day or two to see what can be done.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-12-05T20:44:03+00:00

    It works, and I really appreciate it.

    One problem is when the query word is in a heading. In this case the macro copies the heading and its content many times (from a 10 pages test file it created a 500 pages file).

    I can easily make sure it doesn't happen so it's not a problem. But it brings me to what I ideally would like to do: gathering not paragraphs but headings and their contents. Say I have the * symbol in 3 of my headings (not in the text). I ran your macro and the 3 heading titles containing the * were correctly extracted. How difficult would it be to have their content copied as well? 

    PS: I ran the macro in Outline mode showing headings only and it works differently: the 3 headings titles are still copied but curiously also parts of the text (even though there is no * in the text itself).

    Was this answer helpful?

    0 comments No comments