I need to find sentences and or paragraphs containing specific words (in any order) how would I create the search string for say cpernick or agent ransack

how would I create the search string for say cpernick or agent ransack
that finds sentences and or paragraphs containing specific words (in any order)
Dov_BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
You can use a VBA macro. GetBlockWithWord returns the range of the first block (sentence or paragraph) that contains the given word,
Sub GetBlockWithWordUsage()
    MsgBox GetBlockWithWord(ActiveDocument.Range, "cpernick", wdParagraph)
End Sub

Function GetBlockWithWord(rng As Range, strWord As String, BlockType As WdUnits) As Range
    Select Case BlockType
        Case wdSentence, wdParagraph
            With rng.Find
                .Text = "<" & Trim(strWord) & ">"
                .MatchWildcards = True
                If .Execute Then
                    rng.Expand BlockType
                    Set GetBlockWithWord = rng
                End If
            End With
    End Select
End Function

Open in new window

Dov_BAuthor Commented:
Thank you! but how would I check for multiple words?
Lets say I need to find a sentence or paragraph that contains 2 or more specific words (in any order with any number of different words between them?
Would this be more of an excel advance filter issue if so I am having a dificult time how to write the criteria for such a filter
GrahamSkanRetiredCommented:
This introduces an extra function which looks for a block containing the first word in a list and then checks for any other words. If any of the other words are not found, it moves on to the next block until all the words are found in a block or until  the search range is exhausted.
Sub GetBlockWithWordsUsage()
    MsgBox GetBlockWithWords(ActiveDocument.Range, "cpernick , agent, ransack", wdParagraph)
End Sub

Function GetBlockWithWords(rng As Range, strWords As String, BlockType As WdUnits) As Range
Dim strWord() As String
Dim i As Integer
Dim rngFind0 As Range
Dim rngFind As Range
Dim bFound As Boolean
Dim rngMain As Range

Set rngMain = rng.Duplicate
strWord = Split(strWords, ",")
Set rngFind0 = GetBlockWithWord(rng, strWord(0), BlockType)
Do Until rngFind0 Is Nothing
    bFound = True
    For i = 1 To UBound(strWord)
        Set rngFind = GetBlockWithWord(rngFind0, strWord(i), BlockType)
        If rngFind Is Nothing Then
            bFound = False
            Exit For
        End If
    Next i
    If bFound Then
        Set GetBlockWithWords = rngFind0
        Exit Function
    End If
    rngFind0.Collapse wdCollapseEnd
    rngFind0.End = rngMain.End
Loop
End Function

Function GetBlockWithWord(rng As Range, strWord As String, BlockType As WdUnits) As Range
    Select Case BlockType
        Case wdSentence, wdParagraph
            With rng.Find
                .Text = "<" & Trim(strWord) & ">"
                .MatchWildcards = True
                If .Execute Then
                    rng.Expand BlockType
                    Set GetBlockWithWord = rng
                End If
            End With
    End Select
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.