Private m_WordServer As Word.Application
' Return True if the target is in the file.
Private Function TargetInFile(fname, target) As Boolean
'Dim fnum As Integer
'Dim txt As String
' Open the file.
On Error GoTo OpenError
m_WordServer.Documents.Open FileName:=fname, _
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
' Search for the target string.
With m_WordServer.ActiveWindow.Selection.Find
.ClearFormatting
.Text = target
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
TargetInFile = (m_WordServer.ActiveWindow.Selection = _
target)
' Close the file.
m_WordServer.ActiveDocument.Close False
Exit Function
OpenError:
Exit Function
End Function
' Search all files below the directory that
' match the pattern.
Private Sub CheckFiles(ByVal start_dir As String, ByVal _
pattern As String, ByVal target As String)
Const MAXDWORD = 2 ^ 32
Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim new_files As String
Dim attr As Integer
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim file_size As Double
' Get the matching files in this directory.
' Get the first file.
search_handle = FindFirstFile( _
start_dir & pattern, file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
fname = file_data.cFileName
fname = start_dir & Left$(fname, InStr(fname, _
Chr$(0)) - 1)
If TargetInFile(fname, target) Then
lstFiles.AddItem fname
lstFiles.Refresh
End If
' Get the next file.
If FindNextFile(search_handle, file_data) = 0 _
Then Exit Do
Loop
' Close the file search hanlde.
FindClose search_handle
End If
' Optionally display the new list here.
' Get the list of subdirectories.
search_handle = FindFirstFile( _
start_dir & "*.*", file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
If file_data.dwFileAttributes And DDL_DIRECTORY _
Then
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) _
- 1)
If fname <> "." And fname <> ".." Then
num_dirs = num_dirs + 1
ReDim Preserve dir_names(1 To num_dirs)
dir_names(num_dirs) = fname
End If
End If
' Get the next file.
If FindNextFile(search_handle, file_data) = 0 _
Then Exit Do
Loop
' Close the file search handle.
FindClose search_handle
End If
' Search the subdirectories.
For i = 1 To num_dirs
CheckFiles start_dir & dir_names(i) & "\", pattern, _
target
Next i
End Sub
|