Open Word. "Alt-F11" for the Word Visual Basic Editor.
Insert a module. Paste the following code:
Private Sub FindTextAndHighlight()
'Finds text in Word Documents, opens and highlights
each occurance.
Dim i As Integer, cnt As Integer
Dim FN(500) As String, StringToSearchFor As String,
FilePath As
String
FilePath = "C:\"
StringToSearchFor = InputBox("Search for Text")
Call InitializeFileNamesArray(FilePath,
StringToSearchFor, FN, cnt,
True)
For i = 1 To cnt
Documents.Open FN(i)
Call HighlightsWord(StringToSearchFor, wdYellow,
False)
Next i
End Sub
Sub InitializeFileNamesArray(FilePath As String,
SearchString As String,
FileNamesArray() As String, NumFiles As Integer,
SearchSubfolders As
Boolean)
'Finds files in a filepath containing "SearchString".
'Returns the filenames in an array and a count of
files found.
Dim fs As Object
Dim i As Integer
Set fs = Application.FileSearch
With fs
Dim Path As String
.LookIn = FilePath
.TextOrProperty = SearchString
.MatchTextExactly = True
.SearchSubfolders = SearchSubfolders
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
NumFiles = .FoundFiles.Count
For i = 1 To .FoundFiles.Count
FileNamesArray(i) = .FoundFiles(i)
Next i
Else
NumFiles = 0
End If
End With
End Sub
Sub HighlightsWord(Word2Find As String, Color1 As Long,
MatchCase As
Boolean)
' Highlights Selected Sentence containing "Word2Find".
Highlights it
"Color1" .
With Selection.Find
.ClearFormatting
Do While .Execute(FindText:=Word2Find, Forward:=True,
Format:=True,
MatchCase:=MatchCase, Wrap:=False) = True
Selection.Range.HighlightColorIndex = Color1
Loop
End With
End Sub