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 ha
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
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
I think this will do:
Dim filename As String
filename = Application.GetOpenFilename
Try this
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
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
I think you want this:
Dim filename As String
filename = Application.GetOpenFilename
Dim cell As Range
cell = Application.Range("A1")
cell.Value = filename