![]() |
VBA: Search for word in a file in Folder using VBA?
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 |
Search for word in a file in Folder using VBA?
Are you looking for a folder or are you looking for a file?
-- Jim Cone San Francisco, USA http://www.realezsites.com/bus/primitivesoftware 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 |
Search for word in a file in Folder using VBA?
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. -- Regards, Tom Ogilvy "Jim Cone" wrote in message ... Are you looking for a folder or are you looking for a file? -- Jim Cone San Francisco, USA http://www.realezsites.com/bus/primitivesoftware 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 |
Search for word in a file in Folder using VBA?
or...<g
"but I want to look for a word in a folder" -- Jim Cone San Francisco, USA |
Search for word in a file in Folder using VBA?
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 -- Regards, Tom Ogilvy "Tom Ogilvy" wrote in message ... 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. -- Regards, Tom Ogilvy "Jim Cone" wrote in message ... Are you looking for a folder or are you looking for a file? -- Jim Cone San Francisco, USA http://www.realezsites.com/bus/primitivesoftware 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 |
All times are GMT +1. The time now is 04:39 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com