'Search entire document for String and Select all instances at once
I am new to VBA. I want to search a Word document for all occurrences of the string "the", and select all the occurrences at once.
Currently, I have this code, which finds the string, but I need to run the subroutine over and over. And it doesn't select all occurrences at once.
Sub FindThe()
With Selection.Find
.ClearFormatting
.Text = "the"
.Execute Forward:=True
End With
End Sub
Solution 1:[1]
If I understood your question, you may use a code like this to make whatever format or change you want on the found pieces of text, at once:
Sub FindThe()
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Bold = False 'Does not find a bold text
With .Replacement
.ClearFormatting
.Font.Bold = True 'Format in bold the found texts
.Font.Italic = True 'Format in italic the found texts
.Font.TextColor = wdColorGold 'Format color to Gold for the found texts
End With
'Replace all the ocurrences of "the" with "<<Found THE>>"
.Execute FindText:="the", ReplaceWith:="<<Found THE>>", _
Format:=True, Replace:=wdReplaceAll
End With
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Roberto Santos |