How to get selected path and name of the file opened with file dialog?

后端 未结 12 697
面向向阳花
面向向阳花 2020-12-29 12:14

I need the path name and file name of the file that is opened with File Dialog. I want to show this information with a hyperlink in my worksheet.

With this code I ha

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 13:02

    After searching different websites looking for a solution as to how to separate the full path from the file name once the full one-piece information has been obtained from the Open File Dialog, and seeing how "complex" the solutions given were for an Excel newcomer like me, I wondered if there could be a simpler solution. So I started to work on it on my own and I came to this possibility. (I have no idea if somebody got the same idea before. Being so simple, if somebody has, I excuse myself.)

    Dim fPath As String
    Dim fName As String
    Dim fdString As String
    
    fdString = (the OpenFileDialog.FileName)
    
    'Get just the path by finding the last "\" in the string from the end of it
     fPath = Left(fdString, InStrRev(fdString, "\"))
    
    'Get just the file name by finding the last "\" in the string from the end of it
     fName = Mid(fdString, InStrRev(fdString, "\") + 1)
    
    'Just to check the result
     Msgbox "File path: " & vbLF & fPath & vbLF & vblF & "File name: " & vbLF & fName
    

    AND THAT'S IT!!! Just give it a try, and let me know how it goes...

提交回复
热议问题