FileDialog.InitialFileName only returning Folder, not the file name. (1 Viewer)

willknapp

Registered User.
Local time
Today, 13:13
Joined
Aug 16, 2012
Messages
93
I'm trying to use a file dialog to allow users to select the spreadsheet they want to import, but when I use the following code, I only get the folder path - the file name is left off.

Code:
Private Function GetFileName() as String

    Dim dlg As FileDialog
 
    Set dlg = Application.FileDialog(msoFileDialogFilePicker)
    With dlg
        .Title = "Select the file you want to import:"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Microsoft Excel", "*.xls;*.xlsb;*.xlsm;*.xlsx", 1
        .Filters.Add "All Files", "*.*", 2
        If .Show = True Then
            GetFileName = .InitialFileName
        Else
            GoTo FUNCTION_EXIT
        End If
    End With

FUNCTION_EXIT:
    Exit Function

What am I missing? I assume it's something very simple - I just haven't figure it out yet...
 

boblarson

Smeghead
Local time
Today, 10:13
Joined
Jan 12, 2001
Messages
32,059
Change

GetFileName = .InitialFileName

to

GetFileName = .SelectedItems(1)
 
Last edited:

willknapp

Registered User.
Local time
Today, 13:13
Joined
Aug 16, 2012
Messages
93
Of course, the second I post this, I discover the workaround:

Instead of using
Code:
.InitialFileName
I used
Code:
.SelectedItems(1)

I was under the impression from various online sources that ".InitialFileName" would return the selected file - I guess that's not the case.
 

Users who are viewing this thread

Top Bottom