11

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

使用此代码,我有文件路径:

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

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

4

12 回答 12

13

您可以使用 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
于 2013-08-23T17:43:58.673 回答
12

试试这个

Sub Demo()
    Dim lngCount As Long
    Dim cl As Range

    Set cl = ActiveCell
    ' Open the file dialog
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Show
        ' Display paths of each file selected
        For lngCount = 1 To .SelectedItems.Count
            ' Add Hyperlinks
            cl.Worksheet.Hyperlinks.Add _
                Anchor:=cl, Address:=.SelectedItems(lngCount), _
                TextToDisplay:=.SelectedItems(lngCount)
            ' Add file name
            'cl.Offset(0, 1) = _
            '    Mid(.SelectedItems(lngCount), InStrRev(.SelectedItems(lngCount), "\") + 1)
            ' Add file as formula
            cl.Offset(0, 1).FormulaR1C1 = _
                 "=TRIM(RIGHT(SUBSTITUTE(RC[-1],""\"",REPT("" "",99)),99))"


            Set cl = cl.Offset(1, 0)
        Next lngCount
    End With
End Sub
于 2012-10-02T09:24:59.513 回答
6

要仅从路径中提取文件名,您可以执行以下操作:

varFileName = Mid(fDialog.SelectedItems(1), InStrRev(fDialog.SelectedItems(1), "\") + 1, Len(fDialog.SelectedItems(1)))
于 2015-10-02T12:22:07.910 回答
3

我想你想要这个:

Dim filename As String
filename = Application.GetOpenFilename

Dim cell As Range
cell = Application.Range("A1")
cell.Value = filename
于 2012-10-02T09:19:14.090 回答
1

我认为这是获得您想要的最简单的方法。

感谢 JMK 对第一部分的回答,超链接部分改编自http://msdn.microsoft.com/en-us/library/office/ff822490(v=office.15).aspx

'Gets the entire path to the file including the filename using the open file dialog
Dim filename As String
filename = Application.GetOpenFilename

'Adds a hyperlink to cell b5 in the currently active sheet
With ActiveSheet
 .Hyperlinks.Add Anchor:=.Range("b5"), _
 Address:=filename, _
 ScreenTip:="The screenTIP", _
 TextToDisplay:=filename
End With
于 2014-04-04T06:16:58.353 回答
1

下面的命令足以从对话框中获取文件的路径 -

my_FileName = Application.GetOpenFilename("Excel Files (*.tsv), *.txt")
于 2017-05-21T08:20:16.977 回答
1

FileNameOnly = Dir(.SelectedItems(1))

于 2019-08-16T14:41:51.987 回答
0

代码从根冒号开始文件搜索,如果我想从特定目录开始搜索,以避免每次都去那个目录,我应该放一个。我做到了

Sub GetFilePath()
FileSelected = "G:\Audits\A2010"
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("C14") = FileSelected
End Sub

但它无法从“G:\Audits\A2010”开始到达

于 2014-03-08T09:10:09.857 回答
0

我认为这会做到:

Dim filename As String
filename = Application.GetOpenFilename
于 2014-12-02T08:34:55.710 回答
0

从 Office 2010 开始,我们将无法使用通用对话框控件,因此最好使用 Application 对象来获得所需的结果。

这里我有一个文本框和命令按钮 - 在命令按钮单击事件下粘贴以下代码,这将打开文件对话框并将文件名添加到文本框中。

Dim sFileName  As String

sFileName = Application.GetOpenFilename("MS Excel (*.xlsx), *.xls")

TextBox1.Text = sFileName
于 2015-04-30T07:16:03.837 回答
-1

在搜索了不同的网站以寻找解决方案后,一旦从“打开文件”对话框中获得完整的单件信息,如何将完整路径与文件名分开,并查看给出的解决方案对于 Excel 新手来说是多么“复杂”像我一样,我想知道是否有更简单的解决方案。所以我开始自己研究它,我想到了这种可能性。(我不知道以前是否有人有同样的想法。这么简单,如果有人有,我原谅自己。)

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

就是这样!试一试,告诉我进展如何...

于 2017-03-08T04:23:44.880 回答
-1
Sub GetFilePath()

Set myFile = Application.FileDialog(msoFileDialogOpen)

With myFile

.Title = "Choose File"

.AllowMultiSelect = False

If .Show <> -1 Then

Exit Sub

End If

FileSelected = Replace(.SelectedItems(1), .InitialFileName, "")

End With

ActiveSheet.Range("A1") = FileSelected

End Sub
于 2018-12-18T08:33:18.580 回答