Link to home
Start Free TrialLog in
Avatar of PeterFrb
PeterFrbFlag for United States of America

asked on

VB6 routine to retrieve URL from shortcut

The simple code block below retrieves the target path of a "*.lnk" file: a shortcut pointing to another file or directory to which the user has reference.  In the event that the shortcut is a URL, the extension will be *.url, and the code below will not work.
(The verbiage of "CreateShortcut" here is confusing because if the shortcut already exists, as I intend, "CreateShortcut" will report back on the existing shortcut, not, as the name implies, overwrite the existing one.)
I've built the infrastructure to accomodate a little added challenge: I would like the code below to be dual-purpose.  It should test whether the extension is ".lnk" or ".url" and, in either case, return the shortcut path.  Thanks, ~Peter Ferber

Function RetrieveShortcutPath(strShortcutPath As String) As String
    Dim MyShell As New WshShell
    Dim MyShortcut As WshShortcut
    
    If (instr(ucase(strShortcutPath), ".LNK") > 0) then
        Set MyShortcut = MyShell.CreateShortcut(strShortcutPath)
        RetrieveShortcutPath = MyShortcut.TargetPath
    Else
    End If
End Function

Open in new window

Avatar of PeterFrb
PeterFrb
Flag of United States of America image

ASKER

P.S.  If the object in question requires me to make a reference other than "Windows Script Host Object Model", which allows me to instantiate WshShell and WshShortcut, please specify that as well.
~Peter
ASKER CERTIFIED SOLUTION
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well done, thanks.
You clearly understood the assignment fully and delivered on the answer.  I appreciate your comment indicating that the two choices are "WshShortcut and WshURLShortcut" -- the latter of which I was unaware.  I've added the code as a function, which I personally find useful, by merely modifying your code into a form I find useful.
Thanks,
~Peter
Function RetrieveShortcutPath(strShortcutPath As String) As String
    Dim fso As New FileSystemObject
    Dim wsh As New WshShell
    
    Dim f As File
        
    ' No polymorphism for WshShortcut and WshURLShortcut - except through a generic 'object'.
    Dim o As Object
    
    ' Get the user's 'Desktop'.
    Set o = Nothing
    ' Is this file *Either* link type?
    RetrieveShortcutPath = ""
    If (Right$(strShortcutPath, 4) = ".lnk" Or Right$(strShortcutPath, 4) = ".url") Then
        Set o = wsh.CreateShortcut(strShortcutPath)
        If (Not o Is Nothing) Then
            RetrieveShortcutPath = o.TargetPath
        End If
    End If
End Function

Open in new window

Glad to be of help.

Looks like you can remove the FileSystemObject reference, and the declaration of f As File now?

Oh, and the ' Get the user's 'Desktop' comment.