gpt4 book ai didi

excel - 如何获取使用文件对话框打开的文件的选定路径和名称?

转载 作者:行者123 更新时间:2023-12-01 23:49:01 28 4
gpt4 key购买 nike

我需要使用文件对话框打开的文件的路径名和文件名。我想在我的工作表中通过超链接显示此信息。

通过这段代码,我得到了文件路径:

Sub GetFilePath()

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
End Sub

我仍在寻找获取文件名的方法。

最佳答案

您可以使用 FileSystemObject 获取文件路径的任何部分。 GetFileName(filepath) 给你你想要的。

修改后的代码如下:

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

关于excel - 如何获取使用文件对话框打开的文件的选定路径和名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12687536/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com