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

我需要使用文件对话框(Excel中的VBAmacros)打开的文件的path名和文件名。 我想在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 

有了这个代码我有文件path。 现在我仍然在寻找一种获取文件名的方法。

TX

尝试这个

 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 

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

修改后的代码

 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 

要从path中只提取文件名,您可以执行以下操作:

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

我想你想要这个:

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

我认为这是达到你想要的最简单的方法。

感谢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 

代码从根冒号开始文件search,如果我想从一个特定的目录开始search,避免每次都去那个目录,我应该把它放在哪里。 我喜欢

 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”无法启动

我认为这将做到:

 Dim filename As String filename = Application.GetOpenFilename 

在search不同的网站之后,一旦从“打开文件”对话框中获得完整的单件信息,并且看到为Excel新手提供的解决scheme“复杂”如何从文件名中分离完整path像我一样,我想知道是否可以有一个更简单的解决scheme。 所以我开始自行研究这个问题,然后我开始了这个可能性。 (我不知道是否有人有同样的想法,如果有人这么简单,我原谅自己。)

 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 

这就是它! 试试吧,让我知道它是怎么回事…

从Office 2010中,我们将无法使用通用对话框控件,所以使用Application对象来获得所需的结果是非常好的。

在这里,我得到了一个文本框和命令button – 粘贴下面的代码下命令button单击事件,这将打开文件对话框并将文件名称添加到文本框。

 Dim sFileName As String sFileName = Application.GetOpenFilename("MS Excel (*.xlsx), *.xls") TextBox1.Text = sFileName 

下面的命令足以从对话框中获取文件的path –

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