Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49948

FileDialogFilePick a file, then get the folder path

$
0
0
Hi,

I have what is certainly a very basic question, but which stuck me for hours (not totally lost since I learned quite few interesting things while digging around for the answer ;))

Once a file has been selected, I need to extract the path to go to the parent folder. In the code below, I have removed everything not relevant to my project, so that my problem will hopefully be clear.
I have a way to do it, by removing the n+1 characters from the right of the total path (filename included, n being the length of the filename) but this is not elegant and certainly not the way to do it.

Thank you for your time!


Code:

Sub Test_Get_Info()

Dim FilePath As String, FileToOpen As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = "C:\"
        .AllowMultiSelect = False
           
            If .Show = True Then
                            FileToOpen = .SelectedItems(1)
                            FilePath = '.... (if file picked is C:\Temp\test.xls, FilePath should return: 'C:\Temp'
                            Else: MsgBox "No file specified.", vbExclamation, "CANCEL"
               
            End If
         
        MsgBox FilePath & Application.PathSeparator
    End With

End Sub


Viewing all articles
Browse latest Browse all 49948

Trending Articles