Hey all,
I am trying to write a little code that will search a drive letter for a specific type of file. Both choices are selected by the user.
They select the drive letter then they select the type of file they want to look for.
I have the drive letter part working fine, but the file type returns a "Type Mismatch" error. Can someone take a look at the code and tell me what I'm doing wrong?
ThisWorkbook code:
UserForm1 code:
Thanks!
I am trying to write a little code that will search a drive letter for a specific type of file. Both choices are selected by the user.
They select the drive letter then they select the type of file they want to look for.
I have the drive letter part working fine, but the file type returns a "Type Mismatch" error. Can someone take a look at the code and tell me what I'm doing wrong?
ThisWorkbook code:
Code:
Private Sub Workbook_Open()
Dim fs, d, dc
Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d In dc
UserForm1.cboDriveList.AddItem d.DriveLetter
Next d
With UserForm1.cboFileType
.AddItem (".xls")
.AddItem (".doc")
.AddItem (".jpg")
.AddItem (".csv")
.AddItem (".txt")
.AddItem (".exe")
.AddItem (".pst")
.AddItem (".epf")
.AddItem (".zip")
.AddItem (".mdb")
.AddItem (".pdf")
.AddItem (".htm")
.AddItem (".html")
End With
UserForm1.Show
End Sub
UserForm1 code:
Code:
Private Sub cmdFind_Click()
Dim vaFileName As Variant, lCnt As Long
Dim MyDir As String
Dim MyFile As String
MyDir = UserForm1.cboDriveList.Value & ":\"
MyFile = "*" & UserForm1.cboFileType.Value
'just for me to verify that the variables hold the correct selection
MsgBox "MyDir = " & MyDir, vbOKOnly
MsgBox "MyFile = " & MyFile, vbOKOnly
With Application.FileSearch
.NewSearch
.LookIn = MyDir
.SearchSubFolders = True
.FileType = MyFile
If .Execute > 0 Then
Application.ScreenUpdating = False
For Each vaFileName In .FoundFiles
lCnt = lCnt + 1
Sheet1.Cells(lCnt, 1).Value = vaFileName
Next
Application.ScreenUpdating = True
Else
MsgBox "There were no Excel files found."
End If
End With
UserForm1.Hide
End Sub
Thanks!