how to get file address

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. Windows
I would like to be able to open a dialog box, have the user select the file of interest, and then paste the file address into a particular cell.

I have the macro started, I just don't know the correct language to get the file address. If someone could tell me the code to get that dialogue box to appear and select the file, that would be awesome.

Thanks for the help,

Robert

Here is what I have so far.

Rich (BB code):
Sub PDF_SETUP()

If Range("Z40").Value = "" Then

    Dim mypath As String
    Dim myfile As String
    
    mypath = ThisWorkbook.Path
    
    'Display dialog box to get file address.
    myfile = ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
    Display dialogue box here _    
     "file name here", TextToDisplay:= "file name here.pdf"

    Range("Z40").vlaue = myfile

ElseIf Range("Z40").Value <> "" Then

    Worksheets("Intro Page").Range("Z40").Hyperlinks(1).Follow

End If

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
you can use this to capture the name of the file in the active cell

fn = Application.GetOpenFilename()
fn = Mid(fn, InStrRev(fn, "\") + 1)
ActiveCell.Value = fn
 
Last edited:
Upvote 0
Awesome!! I now get the file name I want placed into cell Z40, but I run into an error when I try to activate the hyperlink. The error message reads:

"Run-time error '9': Subscript out of range".

The code is below with the line flagged in red.

What do I do next?

Rich (BB code):
    fn = Application.GetOpenFilename()
    fn = Mid(fn, InStrRev(fn, "\") + 1)
    
    Range("Z40").Value = fn

'If the link cell is not blank...
ElseIf Range("Z40").Value <> "" Then

    'Open file with link in cell Z40
    Worksheets("Intro Page").Range("Z40").Hyperlinks(1).Follow
    
End If
 
Upvote 0
You entered the filename into the Z40, not the whole path.
The whole path needs to be entered into the Z40's Hyperlink object.
 
Upvote 0
You entered the filename into the Z40, not the whole path.
The whole path needs to be entered into the Z40's Hyperlink object.

Exactly...!!!:)
And for that the code needs to be modified in this way
Code:
fn = Application.GetOpenFilename()
Range("Z40").Value = fn
 
Upvote 0
Thanks. I have modified the code as listed below, and it does put the entire address in the window, but I still get the same error.

Rich (BB code):
If Range("Z40").Value = "" Then

    fn = Application.GetOpenFilename()
    
    Range("Z40").Value = fn

ElseIf Range("Z40").Value <> "" Then

    'Open file with link in cell Z40
    Worksheets("Intro Page").Range("Z40").Hyperlinks(1).Follow
End If
 
Upvote 0
fn = Application.GetOpenFilename()
Range("Z40").Value = fn
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=fn, TextToDisplay:=fn
Worksheets("Intro Page").Range("Z40").Hyperlinks(1).Follow

try using this...
 
Upvote 0
Thanks Niceguy, I found this with some digging

ActiveWorkbook.FollowHyperlink (Range("Z40").value)

It works great.

Thanks to all for the help!!
 
Upvote 0
This worked for me
Code:
Sub test()
    Dim fPath As String, fName As String
    fPath = Application.GetOpenFilename
    fName = Split(fPath, Application.PathSeparator)(UBound(Split(fPath, Application.PathSeparator)))

    With Range("A1").Hyperlinks
        .Delete
        .Add anchor:=Range("A1"), Address:=fPath, TextToDisplay:=fName
    End With
    
    Range("A1").Hyperlinks(1).Follow NewWindow:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top