I want a file picker menu to show Excel wordbooks and CSV files (1 Viewer)

Db-why-not

Registered User.
Local time
Today, 16:06
Joined
Sep 17, 2019
Messages
159
I have a form where you click a button then it opens a windows filepicker dialog box. I want the dialog box to show Excel files and CSV files in the filepicker. Right now only the excel files are showing up in the dialog box. I even tried converting the CSV file to excel file and they still didn't show up. Below is my current code.

Code:
Private Sub cmdSelectFiles2_Click()
' Make sure under Tools Reference- Microsoft Office 15.0 or 16.0 Object Library is checked
Dim fd As FileDialog
Dim item As Variant
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Title = "Please select an Excel Spreadsheet"
.Filters.Clear
.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx"
.Filters.Add "Comma Separated File", "*.csv, *.txt"

If .Show Then
For Each item In .SelectedItems

Me.txtFileNameT = item
Next
End If
End With
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:06
Joined
Oct 29, 2018
Messages
14,095
Hmm, that code looks correct. Are you sure there are CSV or TXT files in the folder? Just curious...

Edit: I just gave your code a try and it seems to work for me. When I have the file type dropdown on XLS, I only see XLS, etc. When I select CSV/TXT, I only see those.

Are you saying you want to see both (XLS and CSV) at the same time? If so, I just added the following line to it.
Code:
.Filters.Add "Both File Types", "*.xls, *.xlsx, *.csv, *.txt"
Hope that helps...
 

Db-why-not

Registered User.
Local time
Today, 16:06
Joined
Sep 17, 2019
Messages
159
the Excel files are showing up, but the CSV files are not showing up. I can try putting a different CSV file in the folder.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:06
Joined
Oct 29, 2018
Messages
14,095
the Excel files are showing up, but the CSV files are not showing up. I can try putting a different CSV file in the folder.
What is the file extension of your CSV files?
 

Db-why-not

Registered User.
Local time
Today, 16:06
Joined
Sep 17, 2019
Messages
159
It is still filtering only excel files. I have attached a picture.
 

Attachments

  • filepicker.JPG
    filepicker.JPG
    60.8 KB · Views: 22

Db-why-not

Registered User.
Local time
Today, 16:06
Joined
Sep 17, 2019
Messages
159
What is the file extension of your CSV files?
My csv file= .csv, I also put a .txt file in there and that isn't showing up. Dialog box is still filtering only .xls or xlsx files
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:06
Joined
Oct 29, 2018
Messages
14,095
It is still filtering only excel files. I have attached a picture.
Hi. I mentioned it eariler, if you want "both" Excel and CSV files at the same time, you need to replace those two filter lines with the one I posted above/earlier. Have you tried that?
 

Db-why-not

Registered User.
Local time
Today, 16:06
Joined
Sep 17, 2019
Messages
159
Hmm, that code looks correct. Are you sure there are CSV or TXT files in the folder? Just curious...

Edit: I just gave your code a try and it seems to work for me. When I have the file type dropdown on XLS, I only see XLS, etc. When I select CSV/TXT, I only see those.

Are you saying you want to see both (XLS and CSV) at the same time? If so, I just added the following line to it.
Code:
.Filters.Add "Both File Types", "*.xls, *.xlsx, *.csv, *.txt"
Hope that helps...
Yes, I want to be able to see both file types. I will try that.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:06
Joined
Oct 29, 2018
Messages
14,095
Yes, I want to be able to see both file types. I will try that.
If you don't need to select the file types, remove the first two filters in your code and just use the one I gave you. This will make sure you can only select XLS and CSV files at the same time. Hope that makes sense...
 

Isaac

Lifelong Learner
Local time
Today, 14:06
Joined
Mar 14, 2017
Messages
3,763
I have a form where you click a button then it opens a windows filepicker dialog box. I want the dialog box to show Excel files and CSV files in the filepicker. Right now only the excel files are showing up in the dialog box. I even tried converting the CSV file to excel file and they still didn't show up. Below is my current code.

Code:
Private Sub cmdSelectFiles2_Click()
' Make sure under Tools Reference- Microsoft Office 15.0 or 16.0 Object Library is checked
Dim fd As FileDialog
Dim item As Variant
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Title = "Please select an Excel Spreadsheet"
.Filters.Clear
.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx"
.Filters.Add "Comma Separated File", "*.csv, *.txt"

If .Show Then
For Each item In .SelectedItems

Me.txtFileNameT = item
Next
End If
End With
End Sub

Notice to the far bottom right there is a drop down showing the various ones you have specified. You just have to change it:

1606866947515.png

So the reason this was happening is because you added 2 separate filters.
You can solve your original problem by putting them as one.
 

Users who are viewing this thread

Top Bottom