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
 

Some videos you may like

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,930
you cant get to a file, unless there is a folder.

("The body cant exist without the mind.")
 

strive4peace

Well-known Member
Joined
Jul 6, 2009
Messages
678
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.
 

HiTechCoach

Well-known Member
Joined
Aug 29, 2010
Messages
1,008
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,167
Messages
5,570,593
Members
412,330
Latest member
carlosjw
Top