Link to home
Start Free TrialLog in
Avatar of cekendricks
cekendricks

asked on

msoFileDialogFilePicker doesn't list files

I have an procedure which is supposed to allow the user to choose which text file is to be imported into the application for processing.  It uses msoFileDialogFilePicker in a seperate sub-procedure to accomplish this.  The problem is that when the user navigates to the proper folder no files are visible in the folder, although they are definitely there.  I have commented out the line in the sub-procedure which originally applied a filter to msoFileDialogFilePicker.  I call the sub with the following lines of code:
MsgBox "Select MLPA4 Text File"
    strMlpa4Path = GetLocation()
    MsgBox "Select MLPA6 Text File"
    strMlpa6Path = GetLocation()

And the sub looks like this:
Public Function GetLocation()

Dim fd As FileDialog
    Dim objfl As Variant
    Dim filename As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        '.Filters.Add "All Files", "*.*"
        .Title = "Choose File to Import"
        .InitialView = msoFileDialogViewDetails
        .Show
    For Each objfl In .SelectedItems
        GetLocation = objfl
    Next objfl
   
    On Error GoTo 0
    End With
End Function
Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

Looks like you have a stray single quote before the filters.add line
try this revision

Public Function GetLocation() as string

Dim fd As FileDialog
    Dim objfl As Variant
    Dim filename As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        '.Filters.Add "All Files", "*.*"
        .Title = "Choose File to Import"
        .InitialView = msoFileDialogViewDetails
       
    if .Show=-1 then
    For Each objfl In .SelectedItems
        GetLocation = objfl
    Next objfl
    end if
    On Error GoTo 0
    End With
End Function

Open in new window

oops, hit the submit button too soon
try this revision
Public Function GetLocation() as string

Dim fd As FileDialog
    Dim objfl As Variant
    Dim filename As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Filters.Add "All Files", "*.*"
        .Title = "Choose File to Import"
        .InitialView = msoFileDialogViewDetails
       
    if .Show=-1 then
    For Each objfl In .SelectedItems
        GetLocation = objfl
    Next objfl
    end if
    On Error GoTo 0
    End With
End Function

Open in new window

Avatar of cekendricks
cekendricks

ASKER

I commented out that line on purpose, because I thought it might be the cause of the problem...I wanted to eliminate filters as the possible cause.  Another thing that I forgot to mention, is that eventhough I have the .filter line commented out, when the filepicker comes up it still lists *.xlsx in the filename box.
I am still getting the same behavior with the revised code.  What puzzles me is the filepicker seems to still be trying to filter for *.xlsx files.  The button lists All files *.*, as I would expect, but the filename box still lists *.xlsx
This works for me.
Since you have set AllowMultiSelect to False
I'm just picking up the one selected file
GetLocation = .SelectedItems(1)

or returning False if the count = 0

Function GetLocation() As String
Dim fd As FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Filters.Add "All Files", "*.*"
        .Title = "Choose File to Import"
        .InitialView = msoFileDialogViewDetails
        .Show

        If .SelectedItems.Count <> 0 Then
            GetLocation = .SelectedItems(1)
        Else
            GetLocation = "False"
        End If
    End With
End Function
Sorry, one more thing

Try FD.Filters.Clear

to get rid of existing filters before you add new ones
Thanks for you suggestion TombStone, but I am still getting the same results.  I cannot seem to clear the .xlsx filter, and since the requested folder only contains .txt files, nothing is showing up.  I have searched ALL of my code to see if there might possible be an old GetLocation function laying around, and there is not...I am now in the process of searching the two involved modules for any references to .xlsx  That's all I can think of to do at this time, other than just adding the code for the GetLocation function to the procedure that is using it.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I've found a solution.  In searching through the rest of my code I noticed that I have another function called SaveLocation.  This is also a file picker type function that I use to select a save location for exported files.  In that function I saw that with fd there is a method .InitialFileName = "*.xlsx"  I thought that that might be what was setting GetLocation's initial file name, so I commented it out of the SaveLocation function.  Well this had no effect.  So I inserted .InitialFileName = "*.*" in my GetLocation function, and 'Lo and Behold', it now see's all the files in the folders.  I will be testing further, but at least it worked the first time I tried it.
.InitialFileName = "*.*" worked for me too!!!! I was going crazy! Thank you!!!!