Specify .FileType from Combobox Selection?

mobiius

New Member
Joined
Mar 30, 2011
Messages
37
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:
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!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Question, which version of Excel are you using as FileSearch has been removed in Version 2007 and 2010.
 
Upvote 0
Here is sample code to look through a folder and list all the files into the activesheet. I think you can adapt it.

Sub FolderDir()

Dim sFilename As String, iRow As Long

sFilename = Dir("M:\Access Files\")
Do Until sFilename = ""
iRow = iRow + 1
Cells(iRow, 1) = sFilename
sFilename = Dir()
Loop

End Sub
 
Upvote 0
Ok, thanks...

I'm not looking to list every file it finds in the drive, I want it to show only the file type that is specified by the user. Listing the files in the sheet is not the issue.

When I manually specify the file type in the ".FileType = " part of the code, it all works. The problem is when I try to pass the value of the combobox selection to the ".FileType" part of the code.

Thanks for replying.

Anyone else?
 
Upvote 0
The code I suggested you look at can be adapted to refer to what you have selected in the combo boxes, it was a sample for you to adapt.

If you have created what you have so far then you would see you can adjust what I sent. If you can't do that then let me know and I will send you the code corrected for you.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top