File search

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?
 
G

Guest

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
 
N

notDave

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
 
N

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
 
G

Guest

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
 
D

Dirk Goldgar

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.
 

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