Daan
Daan

Reputation: 1437

'Find all' method

I'd like to find all text in a document with a certain color and print it in the debug window.

Sub FindText()
    Selection.Find.Font.Color = 3539877
    Selection.Find.Execute
    Debug.Print Selection
End Sub

The problem is that it only gives me only the next result while I want want to print all results at once. As far as I know, a 'FindAll' method is not available. Maybe I can access an array that contains all the find results.

Also, slightly unrelated, would it be possible to copy all results to the clipboard instead of printing them?

Upvotes: 0

Views: 6809

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149325

You have to do the find in a loop. See this example. I am storing the find results in an array

Option Explicit

Sub FindText()
    Dim MyAR() As String
    Dim i As Long

    i = 0

    Selection.HomeKey Unit:=wdStory
    Selection.Find.Font.Color = -671023105

    Do While Selection.Find.Execute = True
        ReDim Preserve MyAR(i)
        MyAR(i) = Selection
        i = i + 1
    Loop

    If i = 0 Then
        MsgBox "No Matches Found"
        Exit Sub
    End If

    For i = LBound(MyAR) To UBound(MyAR)
        Debug.Print MyAR(i)
    Next i
End Sub

Upvotes: 2

Related Questions