2012-10-02 40 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") 
相關問題