Creating shortcut on desktop with Testpartner

Dear All,


I have one solution to create a desktop shortcut for file (file is from C:\ location)  which is working fine on windows 7, windows vista but its failing on windows 8.1 (32 bit). That solution is below in 2 steps -

1. Go on C:\ path

2. Right click on that .exe file and select "send to" text option (using textSelect property) - right key on it to select "Desktop (create shortcut)" option

Now the case is for windows 8.1, its unable to identify "Send to" context menu so I was checking for shell or command prompt command which can fit in my VBA script but that is also not one liner command. Even I found another solution where  we can create bat file or exe file to create short and then use that file in code but this is also not possible as to avoid dependency.

Is there any suggestions on this?


Thanks!

  • Maggie,

    I may be missing the point but why create a shortcut.

    I never use shortcuts ( even when stated by manual scripters.

    The point of a shortcut is normally to run an .exe file, so why not just do that using a shell command

    Shell "C:\Program Files\CliniSys\WinPath Enterprise\Bin\W7Config.exe", vbNormalFocus

    (vbNormalFocus stops program defaulting to minimised state and ending up in the system tray)

    You don't have to hard code the path either :-

     Shell sPublicVariables.gstrTestDataFolder & "CDCalc.exe", vbNormalFocus

    You can supply arguments to the .exe as well

    Shell sPublicVariables.gstrPathTo_LabelPrinting & " /D " & sPublicVariables.DB_Name & "/A  " & strSVC & " /S " & strSite, vbMaximizedFocus

  • Thanks Martin for your reply.

    Actually I just wanted to avoid dependency of any file as my script can run on any machine and then accordingly I need to keep changing path or file existence on machine.

  • Instead of automating Explorer to create the shortcut, you can use the shell's COM API to do it:

    Sub CreateShortcut(path As String, shortcutName As String)
        Dim WshShell As Object
        Set WshShell = CreateObject("WScript.Shell")
        
        Dim desktopPath As String
        desktopPath = WshShell.SpecialFolders("Desktop")
        
        Dim shortcutPath As String
        shortcutPath = desktopPath & "\" & shortcutName & ".lnk"
        
        Dim shellLink As Object
        Set shellLink = WshShell.CreateShortcut(shortcutPath)
        shellLink.TargetPath = path
        shellLink.WindowStyle = 1
        shellLink.WorkingDirectory = desktopPath
        shellLink.Save
    End Sub

    This function takes the file to create the shortcut to as the first parameter, and the name of the shortcut as the second parameter. For example, to create a shortcut to Notepad you would call the function like this:

    CreateShortcut "C:\Windows\notepad.exe", "Notepad"

    I got this from this StackOverflow answer, which was originally written in VBScript.