PDA

View Full Version : Solved: How get TargetPath of .lnk or .url shortcut files?



almeck
05-08-2009, 10:39 AM
I have shortcuts (.lnk and .url) in various folders. I want to get the TargetPath (I think) for such a shortcut, i.e., the file/url the shortcut points to. Having got that, I will use it to create a clickable hyperlink (to the target) in an Excel cell.

Any suggestions would be appreciated.

Emily
05-08-2009, 07:39 PM
Example for desktop


Function Getlnkpath(ByVal Lnk As String)
On Error Resume Next
With CreateObject("Wscript.Shell").CreateShortcut(Lnk)
Getlnkpath = .TargetPath
.Close
End With
End Function


Sub GetLinkPath()
MsgBox Getlnkpath("C:\Users\xxxxxx\Desktop\Emily.lnk")
End Sub

almeck
05-11-2009, 09:49 AM
Thanks, Emily. I have seen this approach before, but when I tried it (at work), I always got an empty string back for the target path.

I happened to get your reply at home, tried it there, and it worked fine!

I believe the difference is that, at work, the shortcuts were contained in a folder on a network drive. Tried it on folders on my Desktop and on other local drives and it worked.

So, it's something about network drive's that's causing the problem. Keeping the folder(s) of shortcuts locally is not a problem, but it'd be nice to know if there was a way to make your code work for shortcuts on a network drive.

Thanks!
Al

Emily
05-12-2009, 06:26 AM
I think you need to find the network drive mapping first
I cannot test due to my PC is stand alone

Here is a WMI method to find the network drive mapping:



Sub NetworkMapDrive()
Set WshNetwork = CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
DrivesStr = "Network drive Mappings:" & Chr(13)
For i = 0 To oDrives.Count - 1 Step 2
DrivesStr = DrivesStr & "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i + 1) & Chr(13)
Next
MsgBox DrivesStr
End Sub

almeck
05-12-2009, 10:32 AM
Most excellent, that did the trick. I had been referring to the network drive as, in my case, "H:" (which is the drive letter I had it mapped to). Using the string returned for H: by the code you sent (instead of "H:"), followed by the path to the shortcut (.lnk or .url) worked.

Thanks, Emily!

Emily
05-13-2009, 05:46 AM
it's my pleasure

Peter Ferber
02-26-2020, 10:39 AM
I am having the same problem, and my TargetPath is always blank. FYI: I am including my routine here. I am not a fan of CreateObject() because I like to use intellisense whenever it's available. I am running on Windows 7 and sending standard files from my C drive, so the latter discussion does not apply to me.

I don't know if this makes a difference, but the links I am testing are to folders, not to files. Intuitively to my mind, this should make no difference. Thank you, ~ Peter Ferber


Function Getlnkpath(ByVal Lnk As String) As String
Dim MyShell As WshShell
Dim MyLink As WshShortcut


On Error Resume Next
Set MyShell = New WshShell
Set MyLink = MyShell.CreateShortcut(Lnk)
With MyLink
Getlnkpath = .TargetPath
.Close
End With
End Function

Paul_Hossler
02-26-2020, 03:17 PM
Welcome to the forum

(and thanks for using CODE tags)

Please take a minute and read the FAQs in my sig

However, this was an 11 year old thread (even if somewhat related) so it's be better to start your own thread using the (+Post New Thread) button top left

What is an example of calling your function?

pstraton
06-01-2022, 09:47 AM
Emily's solution works but the created "Wscript.Shell" object doesn't actually support a Close method. If you comment out the "On Error Resume Next" statement, that call to Close will throw and error. There's no reason to include that call.