File search

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code.

Dim fs As FileSearch
strSearch = "HRP_*"
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strPath
.filename = strSearch
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For intCount = 1 To .FoundFiles.Count
MsgBox .FoundFiles(intCount)
Next intCount
Else
MsgBox "There were no files found."
End If
End With


The error message I receive is at the dimension statement and it says user
type not defined. Does anybody know what the problem might be?
 
I don't know that FileSearch is a valid variable type. When I have used
Application.Filesearch, I dimensioned fs as a variant by just typing:

Dim fs

You may want to try this. I usually find that using the Dir() function is
easier though. You may want to take a look at that function if you haven't
used it.

HTH,

Ted Allen
 
Out of curiosity, I have the following line that checks to
see if a directory exists, then does "whatever".

If Len(Dir("c:\MyDir\", vbDirectory)) = 0 Then
msgbox "Directory does not exist"

How would I need to modify it to check for a file, not a
dir?

~notDave
 
I guess I'll answer my own question here;
If Len(Dir("c:\MyDir\myfile.txt") = 0 Then
msgbox "Give it a good try before posting a question."

~notDave
 
Yes, as you found, that will check for a single file. You can also use
wildcard searches, and you can use Dir() to loop through the results, such as:

StrFileName = Dir("c:\MyDir\*.txt)

Do While StrFileName <> ""
Code to do whatever you want to the current file
StrFileName = Dir() << gets the next file
Loop

Calling Dir() without any arguments returns the next file or folder meeting
the original conditions. There are other arguments that can be used in the
Dir() function as well (such as the vbDirectory used in your original code).

HTH, Ted Allen
 
SHIPP said:
I have the following code.

Dim fs As FileSearch
strSearch = "HRP_*"
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strPath
.filename = strSearch
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For intCount = 1 To .FoundFiles.Count
MsgBox .FoundFiles(intCount)
Next intCount
Else
MsgBox "There were no files found."
End If
End With


The error message I receive is at the dimension statement and it says
user type not defined. Does anybody know what the problem might be?

I believe the FileSearch object is defined in the Microsoft Office
<version> Object Library, so you'd need to set a reference to that
library.
 
Back
Top