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

时间:2012-10-02 09:14:24

标签: excel vba excel-vba

我需要使用文件对话框(excel中的VBA宏)打开的文件的路径名和文件名。我想在excelsheet中用超链接显示这些信息。任何人都可以帮助我吗?

提前致谢

编辑:

这就是我刚发现的:

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

使用此代码我有文件路径。现在我仍在寻找获取文件名的方法。

的Tx

12 个答案:

答案 0 :(得分:11)

您可以使用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

答案 1 :(得分:10)

试试这个

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

答案 2 :(得分:5)

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

varFileName = Mid(fDialog.SelectedItems(1), InStrRev(fDialog.SelectedItems(1), "\") + 1, Len(fDialog.SelectedItems(1)))

答案 3 :(得分:1)

我想你想要这个:

Dim filename As String
filename = Application.GetOpenFilename

Dim cell As Range
cell = Application.Range("A1")
cell.Value = filename

答案 4 :(得分: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

答案 5 :(得分: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”

开始

答案 6 :(得分:0)

我认为这样做:

Dim filename As String
filename = Application.GetOpenFilename

答案 7 :(得分:0)

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

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

Dim sFileName  As String

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

TextBox1.Text = sFileName

答案 8 :(得分:0)

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

my_FileName = Application.GetOpenFilename("Excel Files (*.tsv), *.txt")

答案 9 :(得分:0)

FileNameOnly = Dir(.SelectedItems(1))

答案 10 :(得分:-1)

搜索不同的网站后,一旦从“打开文件”对话框中获取完整的单件信息,并查看&#34;复杂的&#34;以及如何将完整路径与文件名分开,以寻找解决方案。给出的解决方案是像我这样的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

这就是它!试一试,让我知道它是怎么回事......

答案 11 :(得分:-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