File Dialog - Show files only and not folders

Ace71425

Board Regular
Joined
Apr 20, 2015
Messages
130
I have this code below that opens a file dialog box and lets me select files and then writes the filename and filedatetime among other things to table...Everything works great but i'm trying to figure out how to only show files in the dialog box and not folders....In other words it should open up to the default directory of c:\ and show only pdfs, which I have working but it also shows all the folders available in c:\. Thanks!

Code:
Sub Sample()


    Dim f As Object
    Dim db      As Database
    Dim sSQL    As String
    Dim File As String
    Dim FileTime As String
    Set f = Application.FileDialog(msoFileDialogFilePicker)
    Set db = CurrentDb()
    f.AllowMultiSelect = True


    f.Filters.Clear
    f.Filters.Add "PDFS", "*.pdf"
    f.InitialFileName = "c:\"
    
    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            File = Filename(f.SelectedItems(i))
            FileTime = FileDateTime(File)
                sSQL = "INSERT INTO [FaxTest] ([Rep], [Datestamp], [Fax], [Faxdate], [Timestamp]) VALUES(""" & Text0 & """, """ & Text2 & """, """ & File & """, """ & FileTime & """, """ & Text3 & """)"
                db.Execute sSQL, dbFailOnError
        Next
    End If
End Sub


Public Function Filename(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
you cant get to a file, unless there is a folder.

("The body cant exist without the mind.")
 
Upvote 0
adding to ranman's (what is your name) response:

By it's nature, the built-in capabilities allow you to change to other folders. If you want to exclude folders from the dialog box, you will have to build your own. You can do this by looping through the files and getting their names (and anything else you want like Size) and giving users a listbox.
 
Upvote 0
I have this code below that opens a file dialog box and lets me select files and then writes the filename and filedatetime among other things to table...Everything works great but i'm trying to figure out how to only show files in the dialog box and not folders....In other words it should open up to the default directory of c:\ and show only pdfs, which I have working but it also shows all the folders available in c:\. Thanks!

Code:
Sub Sample()


    Dim f As Object
    Dim db      As Database
    Dim sSQL    As String
    Dim File As String
    Dim FileTime As String
    Set f = Application.FileDialog(msoFileDialogFilePicker)
    Set db = CurrentDb()
    f.AllowMultiSelect = True


    f.Filters.Clear
    f.Filters.Add "PDFS", "*.pdf"
    f.InitialFileName = "c:\"
    
    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            File = Filename(f.SelectedItems(i))
            FileTime = FileDateTime(File)
                sSQL = "INSERT INTO [FaxTest] ([Rep], [Datestamp], [Fax], [Faxdate], [Timestamp]) VALUES(""" & Text0 & """, """ & Text2 & """, """ & File & """, """ & FileTime & """, """ & Text3 & """)"
                db.Execute sSQL, dbFailOnError
        Next
    End If
End Sub


Public Function Filename(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

Are you only wanting to add files even if the user selects all which might include folders?

If yes, then I would test what type each item is before adding to your table.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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