VBA: Search for word in a file in Folder using VBA?

M

myworld726

Hi,

Does anyone know how to search for a word in files in a folder using
VBA? I know the code to search and return folder names but I want to
look for a word in a folder like in using windows search function to
"find a word or phrase" in a file.


Thanks a lot
MW
 
J

Jim Cone

Are you looking for a folder or are you looking for a file?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


<[email protected]>
wrote in message
Hi,
Does anyone know how to search for a word in files in a folder using
VBA? I know the code to search and return folder names but I want to
look for a word in a folder like in using windows search function to
"find a word or phrase" in a file.
Thanks a lot
MW
 
T

Tom Ogilvy

search for a word in files in a folder

I take it he is looking in the content of the files in a folder to see if
they contain a specified word.

I would see it as a propertytest setting in the FileSearch object, but it is
poorly documented.

Sub AA()Dim fs As FileSearchSet fs = Application.FileSearchfs.NewSearch With
fs With .PropertyTests .Add Name:="Text or Property", _
Condition:=msoConditionIncludesPhrase, _ Value:="horse" End With
..LookIn = "E:\Data" .SearchSubFolders = False .Filename = "*"
..MatchTextExactly = False .FileType = msoFileTypeAllFilesEnd With If
fs.Execute() > 0 Then MsgBox "There were " & fs.FoundFiles.Count & _
" file(s) found." For i = 1 To fs.FoundFiles.Count MsgBox
fs.FoundFiles(i) Next i Else MsgBox "There were no files
found." End IfEnd Subdid work for me looking for the word "horse" as an
example.
 
T

Tom Ogilvy

That came out a bit jumbled:

Sub AA()
Dim fs As FileSearch
Set fs = Application.FileSearch
fs.NewSearch
With fs
With .PropertyTests
.Add _
Name:="Text or Property", _
Condition:=msoConditionIncludesPhrase, _
Value:="horse"
End With
.LookIn = "E:\Data"
.SearchSubFolders = False
.Filename = "*"
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
End With
If fs.Execute() > 0 Then
MsgBox "There were " & _
fs.FoundFiles.Count & _
" file(s) found."
For i = 1 To fs.FoundFiles.Count
msgbox fs.FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top