We help IT Professionals succeed at work.

Checking / Creating a desktop shortcut

Andy Brown
Andy Brown asked
on
I have an Access 2003 database that uses the Sagekey installation wizzard.  The problem is, that when the users run the install, they are not installing an Access database on their PC, just the runtime and registry setup.  The MDE file that they are ultimately loading is on the server, which means you can't use the Sagekey software to generate the shortcut.

My question is this - through VBA can you reliably detect if a the user has the application shortcut on the the desktop?  If not I want it to be created on the fly, which I think I can do.

Any help would be greatly appreciated.
Comment
Watch Question

use the following macro:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=584

It will create a shortcut if it does not exist. If it exists it will just overwrite with the same thing.
Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
Commented:
I'd use the Windows Scripting Shell instead:
Dim WSHShell As Object
Set WSHShell = CreateObject("WScript.Shell")

Dim MyShortcut As Object
Dim MyDesktopPath As String

'/ Get the path to the Desktop. If you want to save the shortcut somewhere else
'/ you'd change this path.
MyDesktopPath = WSHShell.SpecialFolders("Desktop")

'/ This actually creates the shortcut object
Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\MyShortcutName.lnk")

MyShortcut.TargetPath = "Full path to your program"
MyShortcut.WorkingDirectory = "Full path to your working directory"
'/ This saves the shortcut
MyShortcut.Save

Set MyShortcut = Nothing
Set WSHShell = Nothing

Open in new window

If you want to include an Icon, use the .IconLocation setting:
MyShortcut.IconLocation = "path to your icon file"

If you want to define how the program opens (i.e. Maximized, Minimized, etc) use the .WindowStyle setting:
MyShortcut.WindowStyle = 4

The value of 4 = "Displays a window in its most recent size and position. The active window remains active."

This comes from the "Running Program" article: http://technet.microsoft.com/en-us/library/ee156605.aspx

TechNet article about "Working With Shortcuts": http://technet.microsoft.com/en-us/library/ee156583.aspx

KB Article about scripting and shortcuts: http://support.microsoft.com/kb/244677

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Note too that the only way to check if a shortcut exists is if you know the name and location of the shortcut. For example, if I wanted to see if a shortcut named MyAccessShortcut.lnk existed on the desktop I could do so, but I couldn't determine if the same shortcut existed on the Start Menu, or in the Program Menu unless i also checked those locations. Also, if Windows has renamed it to MyAccessShortcut2.lnk, then the routine would not locate that one.

In general, it's best to just create the shortcut and let the users decide if they want to keep them. Even the most unsophisticated Windows users know how to delete a shortcut!
Andy BrownDeveloper

Author

Commented:
Superb - that works a treat.