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

后端 未结 12 738
面向向阳花
面向向阳花 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:14

    You can get any part of the file path using the FileSystemObject. GetFileName(filepath) gives you what you want.

    Modified code below:

    Sub GetFilePath()
    Dim objFSO as New FileSystemObject
    
    Set myFile = Application.FileDialog(msoFileDialogOpen)
    With myFile
    .Title = "Choose File"
    .AllowMultiSelect = False
    If .Show <> -1 Then
    Exit Sub
    End If
    FileSelected = .SelectedItems(1)
    End With
    
    ActiveSheet.Range("A1") = FileSelected 'The file path
    ActiveSheet.Range("A2") = objFSO.GetFileName(FileSelected) 'The file name
    End Sub
    

提交回复
热议问题