• Shortcut Properties (Access 97)

    Author
    Topic
    #392173

    Is there a way to access shortcut properties, specifically the Tarket string? I would like to edit the string and save the new string.

    Thanks,

    James

    Viewing 0 reply threads
    Author
    Replies
    • #703077

      The easiest way to do this is use Windows Script Host (WSH). Simple example:

      Public Sub CreateDesktopShortcut(ByRef strShortcut As String)

      Dim wsh As New IWshRuntimeLibrary.IWshShell_Class
      Dim shrt As New IWshShortcut_Class
      Dim strDesktopPath As String
      Dim strTarget As String

      strDesktopPath = wsh.SpecialFolders(“Desktop”)
      strTarget = “C:Program FilesMicrosoft OfficeOfficeWINWORD.EXE”

      Set shrt = wsh.CreateShortcut(strDesktopPath & “” & strShortcut & “.lnk”)

      With shrt
      .TargetPath = Chr(34) & strTarget & Chr(34)
      .Arguments = Chr(34) & “C:My DocumentsTest.doc” & Chr(34)
      .Hotkey = “CTRL+SHIFT+W”
      .IconLocation = strTarget & “, 0”
      .Description = strShortcut
      .Save
      End With

      Set wsh = Nothing
      Set shrt = Nothing

      End Sub

      You can use WSH CreateShortcut method to create new shortcut, or to modify existing one. Above example worked in either case. Note use of WSH SpecialFolders property to get Desktop path. Also note use of Chr(34) to delimit target path with double-quotes, which are required if any spaces in path. To use this example you need to set a reference to the Windows Script Host Object Model (Ver 1.0) (IWshRuntimeLibrary) (WSHOM.OCX). For more info on WSH properties/methods refer to MSDN reference (look up CreateShortcut under WSH/Reference/Methods):

      Windows Script Host Object Model

      Examples are in VBScript but can be modified for use in VBA.

      HTH

      • #703351

        Thanks

      • #703788

        Thanks again for the example and the information.

        How would I modify the example to capture the current Target of a shortcut? I want to test for a string before I modify it.

        Thanks

      • #703822

        I can get part of the Target of the shortcut with the following:

        Set shrt = wsh.CreateShortcut(strDesktopPath)

        strTarket = shrt.TargetPath

        shrt.TargetPath is returning everything within the first set of quotes. Any ideas on how to capture the MDB and MDW information which are inclosed in quotes of their own?

      • #703841

        Got it!

        • #703856

          As you probably figured out, the shortcut Arguments property will contain any command line arguments, such as path to workgroup file, that follow TargetPath. Example of how you can retrieve these properties with existing shortcut, for anyone interested:

          Public Sub GetShortcutProperties(ByRef strShortcut As String)
          On Error GoTo Err_Handler

          Dim wsh As New IWshRuntimeLibrary.IWshShell_Class
          Dim lnk As New IWshShortcut_Class
          Dim fso As New IWshRuntimeLibrary.FileSystemObject
          Dim strDesktopPath As String
          Dim strTarget As String
          Dim strArgs As String
          Dim strMsg As String

          strDesktopPath = wsh.SpecialFolders(“Desktop”)

          If fso.FileExists(strDesktopPath & “” & strShortcut & “.lnk”) Then
          Set lnk = wsh.CreateShortcut(strDesktopPath & “” & strShortcut & “.lnk”)
          ‘ Get shortcut properties:
          strTarget = lnk.TargetPath
          strArgs = lnk.Arguments
          Debug.Print “Target: ” & strTarget
          Debug.Print “Arguments: ” & strArgs
          ‘ Change properties if necessary, then save
          Else
          strMsg = “Shortcut not found.”
          MsgBox strMsg, vbExclamation, “NOT FOUND”
          ‘ Create new shortcut here if necessary, then save
          End If

          Exit_Sub:
          Set wsh = Nothing
          Set lnk = Nothing
          Set fso = Nothing
          Exit Sub
          Err_Handler:
          Select Case Err.Number
          Case 0
          Resume Next
          Case Else
          strMsg = “Error No ” & Err.Number & “: ” & Err.Description
          MsgBox strMsg, vbExclamation, “SHORTCUT ERROR”
          Resume Exit_Sub
          End Select

          End Sub

          Example of use:

          GetShortcutProperties “Shortcut to Northwind”
          Target: C:Program FilesMicrosoft OfficeOffice10MSACCESS.EXE
          Arguments: “C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb”

          Note that the Arguments property included the quotes, but not the TargetPath. (I used Chr(34) for both when creating shortcut.) But in any event you can test shortcut properties & modify if necessary by using WSH.

          HTH

    Viewing 0 reply threads
    Reply To: Shortcut Properties (Access 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: