Consulting

Results 1 to 9 of 9

Thread: Solved: How get TargetPath of .lnk or .url shortcut files?

  1. #1

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

    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.

  2. #2
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Example for desktop

    [vba]
    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
    [/vba]

  3. #3
    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

  4. #4
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    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

  5. #5
    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!

  6. #6
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    it's my pleasure

  7. #7
    VBAX Newbie
    Joined
    Feb 2020
    Location
    El Cerrito, CA
    Posts
    1
    Location
    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

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    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?
    Last edited by Paul_Hossler; 02-27-2020 at 08:49 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    4
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •