2012-10-02 45 views
6

我需要打开与文件对话框(在Excel VBA宏)的文件的路径名和文件名的文件的路径和名称。我想用我的Excel工作表中的超链接显示这些信息。谁能帮我?如何获得选择使用打开文件对话框控制

在此先感谢

编辑:

这是我刚发现:

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

回答

8

试试这个

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 
+0

呵呵,我删除了我的帖子,因为它与你的相同。 OP可以放心使用:-) – Trace

+0

Tx,这正是我需要的文件路径部分! :D – VeVi

+0

@ user1346347编辑也返回文件名 –

1

我想你想要这样的:

Dim filename As String 
filename = Application.GetOpenFilename 

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

的Tx对你的反应。我想我没有让自己很清楚。我需要打开一个文件。该文件中我需要知道文件路径和文件名。我找到了获取文件路径的解决方案。我在我的问题中添加它。 – VeVi

+0

更新了我的回答 – JMK

+0

我没有得到我的文件名,也许是因为我使用FileDialog? – VeVi

8

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

修改下面的代码:

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

但它无法启动REACH法规“G:\审计\ A2010”

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 
0

我认为这会做:

Dim filename As String 
filename = Application.GetOpenFilename 
0

从2010年的办公室,我们将不能够使用通用对话框控件,所以很好用Application对象来获得所需的结果。

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

Dim sFileName As String 

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

TextBox1.Text = sFileName 
3

要从路径只提取文件名,你可以做到以下几点:

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

简单的一条线解决方案。大!正是我需要的。 – riderBill

0

搜索不同的网站寻找一个解决方案,如何完整路径与文件名,一旦分开后全单件信息已经从打开文件对话框得到,看到给出的解决方案是如何“复杂”是一个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 

这就是全部!只要给它一个尝试,让我知道如何去...

+0

上帝帮助我!我刚刚意识到法比奥雷切已经给出了相同的解决方案(10月2日,15),虽然不像我的完整,但相同,正好在我的下面!可能是因为其简洁,只有一行,我没有看到它。我向你道歉,法比奥。 –

+0

此解决方案不能在Excel(2016)中编译。它不喜欢这行:'fdString =(OpenFileDialog.FileName)'我不熟悉''作为关键字。另外,对于OpenFileDialog,是否需要启用参考? – GlennFromIowa

0

下面的命令,就足以从一个对话框,获取该文件的路径 -

my_FileName = Application.GetOpenFilename("Excel Files (*.tsv), *.txt") 
相关问题