Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB6 routine to retrieve URL from shortcut

Posted on 2009-05-12
5
Medium Priority
?
1,027 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:PeterFrb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:PeterFrb
ID: 24368955
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
0
 
LVL 10

Accepted Solution

by:
peetm earned 2000 total points
ID: 24372158
A bit cludgy but ... here goes:

I'm using a generic Object to save using two derived types - WshShortcut and WshURLShortcut.

Object is polymorphic with ALL object types, and as both the ones you're interested in have the 'TargetPath' property, it seems suitable.


The 'file stuff' is handled by a FileSystemObject - add the 'Microsoft Scripting Runtime' as a reference.

    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'.
    '
    For Each f In fso.GetFolder(fso.BuildPath(Environ("HOMEPATH"), "Desktop")).Files
    
        Set o = Nothing
    
        ' Is this file *Either* link type?
        '
        If Right$(f.Path, 4) = ".lnk" Or Right$(f.Path, 4) = ".url" Then
        
            Set o = wsh.CreateShortcut(f.Path)
        
            If Not o Is Nothing Then
            
                Debug.Print o.TargetPath
            
            End If
        
        End If
    
    Next f

Open in new window

0
 

Author Closing Comment

by:PeterFrb
ID: 31580714
Well done, thanks.
0
 

Author Comment

by:PeterFrb
ID: 24377377
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

0
 
LVL 10

Expert Comment

by:peetm
ID: 24377625
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.

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question