Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Determining the target of a shortcut (VBA 6 Word 2K)

    Good afternoon;
    Surely someone out there has solved this little problem. I am attempting to determine the target to which a shortcut points.

    The following code "test1" allows me to determine the properties of a Word Document; yet Procedure "test2" raises
    "Run-time error '424'
    Object required"

    Public Sub test1()
    Dim MyObject As Object
    Set MyObject = GetObject("CATA DriveApproach.doc")
    End Sub


    Public Sub test2()
    Dim MyObject As Object
    Set MyObject = GetObject("CATA DriveShortcut to Approach.doc.lnk")
    End Sub

    TIA
    Regards
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Determining the target of a shortcut (VBA 6 Word 2K)

    The following function will return the target of a shortcut:

    Public Function GetShortcutTarget(strShortcut As String) As String
    Dim objShell As Object, objLink As Object
    On Error GoTo ErrHandler

    Set objShell = CreateObject("WScript.Shell")
    Set objLink = objShell.CreateShortcut(strShortcut)
    GetShortcutTarget = objLink.TargetPath

    ExitHandler:
    On Error Resume Next
    Set objLink = Nothing
    Set objShell = Nothing
    Exit Function

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Function

    It uses scripting, but with late binding, so you do not need to set a special reference to use it. Example:

    Debug.Print GetShortcutTarget("CATA DriveShortcut to Approach.doc.lnk")

  3. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Determining the target of a shortcut (VBA 6 Word 2K)

    Hans

    As always; many many thanks.

    It works a treat.
    Regards
    Don

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the target of a shortcut (VBA 6 Wo

    >The following function will return the target of a shortcut ...
    ... and the following code will make use of it, to replace all shortcut links with their targets.
    I tested this, briefly, on my "favourites" folder of MP3 tracks, replacing my accumulation of shortcut links with the actual files,
    if only to see how close I was to the threshhold size of a CD prior to burning.
    Full module in the attached TXT file.<pre>Function ReplaceShortcutWithCopy(strFolder As String)
    Dim strFileName As String
    strFileName = Dir(strFolder)
    While Len(strFileName) > 0
    Dim strTarget As String
    strTarget = GetShortcutTarget(strFolder & strFileName)
    If Len(strTarget) > 0 Then
    If blnFileExists(strTarget) Then
    FileCopy strTarget, strFolder & strGetNameExtent(strTarget)
    Kill strFolder & strFileName
    Else
    End If
    Else
    End If
    strFileName = Dir
    Wend
    'Sub TESTReplaceShortcutWithCopy()
    ' Dim strFolder As String
    ' strFolder = "Cocuments and SettingsChrisBDesktopfavourites"
    ' Call ReplaceShortcutWithCopy(strFolder)
    'End Sub
    End Function
    </pre>

    Attached Files Attached Files

Posting Permissions

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