Creating Shortcuts

When you start distributing your VBA applications, users will likely request that you automatically place a shortcut to your application on their desktop. VBA does not provide a way to create Windows shortcuts. Luckily for you, you now know how to work with WSH, and you can use its Shell object to create shortcuts to applications or web sites without any user intervention. The WshShell object exposes the CreateShortcut method, which you can use in the following way:

Set myShortcut = WshShell.CreateShortcut(Pathname)

Pathname is a string indicating the full path to the shortcut file. All shortcut files have the .lnk extension, and this extension must be included in the pathname. The CreateShortcut method returns a shortcut object that exposes a number of properties and one method, as shown in Table 8-8. Table 8-8: Properties/methods of the CreateShortcut object Property/Method Description

TargetPath The TargetPath property is the path to the shortcut's executable.

WshShell.TargetPath = ActiveWorkbook.FullName WindowStyle The WindowStyle property identifies the type of window style used by a shortcut. 1 - Normal window 3 - Maximized window 7 - Minimized window

WshShell.WindowStyle =

Property/Method Description

The HotKey property is a keyboard shortcut (for example, Alt+f, Shift+g, Ctrl+Shift+z, etc.)

WshShell.Hotkey = "Ctrl+Alt+w" The IconLocation property is the icon location of the shortcut. Because icon files usually contain more than one icon, you should provide the path to the icon file followed by the index of the icon number in this file. If not specified, Windows uses the default icon for the file.

WshShell.IconLocation = "notepad.exe, 0" The Description property contains a string value describing a shortcut.

WshShell.Description = "Wordware Web Site" WorkingDirectory The WorkingDirectory property identifies the working directory used by a shortcut.

strWorkDir = WshShell.SpecialFolders("Desktop") WshShell.WorkingDirectory = strWorkDir Save This is the only method of the Shortcut object. After using the

CreateShortcut method to create a shortcut object and set the shortcut object's properties, the Save method must be used to save the shortcut object to disk.

Creating a shortcut is a three-step process:

1. Create an instance of a WshShortcut object.

2. Initialize its properties (see Table 8-8 above).

3. Save it to disk with the Save method.

The following example creates a WshShell object and uses the CreateShortcut method to create two shortcuts: a Windows shortcut to the active Microsoft Excel workbook file and an Internet shortcut to the Wordware Publishing web site. Both shortcuts are placed on the user's desktop. The procedure uses the SpecialFolders property of the WshShell object to return the path to the Windows desktop.

Sub CreateShortcut()

' this script creates two desktop shortcuts

Dim WshShell As Object

Dim objShortcut As Object

Set WshShell = CreateObject("WScript.Shell")

' create an internet shortcut

Set objShortcut = WshShell.CreateShortcut(WshShell. _

SpecialFolders("Desktop") & "\Wordware.url") objShortcut.TargetPath = "http://www.wordware.com" objShortcut.Save ' create a file shortcut

Set objShortcut = WshShell.CreateShortcut(WshShell. _

SpecialFolders("Desktop") & "\" & ActiveWorkbook.Name & ".lnk") With objShortcut

.TargetPath = ActiveWorkbook.FullName .WindowStyle = 7

HotKey

IconLocation

Description

.Save End With

Set objShortcut = Nothing Set WshShell = Nothing End Sub

0 0

Post a comment