I need the path name and file name of the file that is opened with File Dialog. I want to show this information with a hyperlink in my worksheet.
With this code I have the file path:
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
I'm still looking for a way to get the filename.
I think this is the simplest way to get to what you want.
Credit to JMK's answer for the first part, and the hyperlink part was adapted from 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
After searching different websites looking for a solution as to how to separate the full path from the file name once the full one-piece information has been obtained from the Open File Dialog, and seeing how "complex" the solutions given were for an Excel newcomer like me, I wondered if there could be a simpler solution. So I started to work on it on my own and I came to this possibility. (I have no idea if somebody got the same idea before. Being so simple, if somebody has, I excuse myself.)
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
AND THAT'S IT!!! Just give it a try, and let me know how it goes...
The below command is enough to get the path of the file from a dialog box -
my_FileName = Application.GetOpenFilename("Excel Files (*.tsv), *.txt")
You can get any part of the file path using the FileSystemObject. GetFileName(filepath) gives you what you want.
Modified code below:
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
I think you want this:
Dim filename As String
filename = Application.GetOpenFilename
Dim cell As Range
cell = Application.Range("A1")
cell.Value = filename
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
FileNameOnly = Dir(.SelectedItems(1))
From office 2010, we won't be able to use the common dialog box control, so it's nice to use the Application object to get the desired results.
Here I got a text box and Command button - paste the following code under the command button click event, which will open the file dialog box and add the File name to the Text box.
Dim sFileName As String
sFileName = Application.GetOpenFilename("MS Excel (*.xlsx), *.xls")
TextBox1.Text = sFileName
To extract only the filename from the path, you can do the following:
varFileName = Mid(fDialog.SelectedItems(1), InStrRev(fDialog.SelectedItems(1), "\") + 1, Len(fDialog.SelectedItems(1)))
I think this will do:
Dim filename As String
filename = Application.GetOpenFilename
The code starts file search from root colon, If I want to start search from a specific directory, to avoid going to that directory every time, where I should put one. I did it like
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
But it could not start reach from "G:\Audits\A2010"
Source: Stackoverflow.com