Reputation: 9546
I want open a dialog where the user can:
I'm trying to use VBA's Application.FileDialog to accomplish this, current code as follows:
Sub makeFileDialog()
Dim dialog As FileDialog
Dim result As String
Set dialog = Application.FileDialog(msoFileDialogFolderPicker)
With dialog
.InitialFileName = "c:\"
.InitialView = msoFileDialogViewDetails
If dialog.Show = -1 Then
result = .SelectedItems.Item(1)
Else
result = ""
End If
End With
debug.print result
End Sub
This lets me select a folder, but the .InitialView
isn't properly set--the views button is grayed out, and the folder list isn't divided into columns for sorting. I think maybe .InitialView
can't be set with msoFileDialogFolderPicker
. Also, no filenames show up.
I tried changing FileDialog
's parameter to msoFileDialogFilePicker
, which let me use views and showed files, but with that I can't select and return a folder.
I saw a solution online that uses the CreateObject("Shell.Application")
, but the dialog created by the shell isn't very flexible and doesn't offer much information about each file.
Ideas?
Upvotes: 0
Views: 7434
Reputation: 14685
Short answer: No. There is not way to allow a user to select either a folder or file. I think most would agree that this is a good thing.
You have it right - use msoFileDialogFilePicker to show all folders and files. You can, if you so desire, just use the filepicker, and parse the file name to get the folder path if you really need to.
Upvotes: 1