FileSearch Idiosyncracies

R

robot

Hello,

I have a folder consisting of Word documents, and frequently need to
retrieve files containing some given text.
So I solicit the help of VBA and cook up the following subroutine for
testing:

Sub Test()
Dim fs As FileSearch
Set fs = Application.FileSearch
With fs
.NewSearch
.FileType = msoFileTypeWordDocuments
.FileName = "*.doc"
.TextOrProperty = InputBox("Input Search String")
.LookIn = "c:\job\04\04\test\"
.SearchSubFolders = False
End with
Set fs=nothing

MsgBox .Execute()
' returns the number of files found

End Sub

In the LookIn folder ("c:\job\04\04\test\"), there are 2 doc files, each
containing the word "gobble'.
However, when I input "gobble" in the input box, the .execute method returns
0 files.
I have tried inputing "*gobble*", "gobbl*", but to no avail.
What have gone wrong?
Suggestions are most welcome!
 
P

Peter Hewett

Hi robot

This works for me:

Public Sub FindSomeDocuments()
Dim docToModify As Word.Document
Dim lngindex As Long

' Set the folder to search and type of file
With Application.FileSearch
.NewSearch
.LookIn = "c:\my documents\test\"
.SearchSubFolders = False
.FileName = "*.doc"
.FileType = msoFileTypeWordDocuments
.TextOrProperty = "test"
If .Execute() > 0 Then
For lngindex = 1 To .FoundFiles.Count

' *** Execute your code here ***
Debug.Print .FoundFiles(lngindex)

' Save and close updated document
Next lngindex
Application.ScreenUpdating = True
Else
MsgBox "There are no files is the search path that match the search criteria"
End If
End With
End Sub

HTH + Cheers - Peter
 
R

robot

Hi Peter,

Thank you for your reply.
I should have made myself clearer: The subroutine works (the line 'set
fs=nothing' should be deleted. My mistake.) most of the time, but some
search strings, such as 'gobble', 'abc', etc, cause problems: They are in
the documents, yet for some reasons they cannot be found by the FileSearch
object.
 
P

Peter Hewett

Hi robot

During my testing of this in the past I've not found any anomalies when specifying the
TextOrProperty. I know this doesn't help but it does what I expect during testing.

Is their anything out of the ordinary about the text you are searching for? Is it in the
body of the document, the result of a field??? I'm just trying to work through what may
be going on at your end as so far I can't reproduce it.

Cheers - Peter


Hi Peter,

Thank you for your reply.
I should have made myself clearer: The subroutine works (the line 'set
fs=nothing' should be deleted. My mistake.) most of the time, but some
search strings, such as 'gobble', 'abc', etc, cause problems: They are in
the documents, yet for some reasons they cannot be found by the FileSearch
object.

HTH + Cheers - Peter
 
R

robot

Hi Peter,

I still have problem with 'Gobble'. It is plain old text that I typed in a
document for testing.
I also tried searching for 'Gobbl', 'Gobb', 'Gob'. All return nil. But when
I searched for 'Go', both the said document and another document that
*doesn't* contain the string 'Go' are returned!!
I wonder if anyone has ever encountered the same problem.

BTW, I use Win XP and Office XP standard.
 
P

Peter Hewett

Hi robot

I've done further investigation. The FileSearch objects TextOrProperty property only
matched whole words (I have not tested to determine *exactly* what a "word" is). So if
you search for "Gobbl", "Gobb" or "Gob" and the text in the document is "Gobble" then your
not going to get a successful match. You can use wildcards in your search string. So
"Gob*" will match "Gobble", "Gobbles" and "Gobbled". Or "Gobble?" with match "Gobbles",
"Gobbled" but not "Gobble".

The text comparison is case insensitive so "Gobble" = "goBBLe". At this stage the problem
may well be what constitutes a "word" boundary.

HTH + Cheers - Peter
 
R

robot

Hi Peter,

I have tried wildcards (see my first posting) also, but unfortunately that
doesn't help.
At this moment I have no clue what's going on :-(
 

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