FileSearch - cannot get to work

  • Thread starter Thread starter Howard Jacobs
  • Start date Start date
H

Howard Jacobs

I am having trouble with FileSearch:

With Application.FileSearch
For i = 1 To .SearchFolders.Count
.SearchFolders.Remove i
Next i

.NewSearch
.LookIn = "C:\My Documents"
.SearchSubFolders = True
.FileName = "*.*"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles ' *******************

MsgBox "You are about to search for " & .FileTypes.Count & _
" file types."

If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If

End With

The above code does not compile even though it is copied from "Help" -
msoFileTypeAllFiles is "Variable undefined".

If I omit that line the search returns 0 files.

Pls advise

HJ
 
msoFileTypeAllFiles

This constant is declared in the Microsoft Office X.X Object Library
(X.X being your version number), so the fact that it's not compiling
suggests either that you don't have a reference to the library or that
it's broken.

Go to Tools|References and set a reference to the library.
ALternatively, you could replace msoFileTypeAllFiles with its literal
value, 1.
 
You were right on; I registered the Object & it compiled OK.

Thank you for your help.

HJ

BTW 2 oddities, I thought:-

1. .LookIn = "C:\My Documents" doesn't work.
I had to use "C:\Documents and Settings\Administrator\My Documents"

2. I thought that you had to use \\ to get a \ in strings but
"C:\Documents and Settings" & "C:\\Documents and Settings" both work
the same.
 
BTW 2 oddities, I thought:-

1. .LookIn = "C:\My Documents" doesn't work.
I had to use "C:\Documents and Settings\Administrator\My Documents"

This is normal behaviour for Windows. "C:\My Documents" specifies *a*
folder called "My Documents" in the root of the the C: drive, not the
user's actual "My Documents" folder.

In standard English-language installations of Windows you can normally
get the user's My Documents with

strMyDocsFolder = Environ("USERPROFILE") & "\My Documents"

but I don't think you can be 100% certain that the My Documents folder
is actually called My Documents, or indeed that %USERPROFILE% has not
been tampered with, so it's probably safer to use

set WshShell = CreateObject("WScript.Shell")
strMyDocsFolder WshShell.SpecialFolders("MyDocuments")

or the Windows API function SHGetSpecialFolderLocation().
2. I thought that you had to use \\ to get a \ in strings but
"C:\Documents and Settings" & "C:\\Documents and Settings" both work
the same.

In C-like languages you have to escape a backslash with a backslash, but
not in VBA. (IIRC backslashes in Word fields also need to be doubled.)
 
Back
Top