Posts Tagged ‘short cuts’


Shortcuts with VB Script


Sometimes I need to create shortcuts with VB Script especially if the script is being installed and a shortcut needs to be created to the script.  The code to create a shortcut is quite straight forward,  but the CreateShortcut() function presented here pulls everything together into a single reusable form.  Optional parameters that you don’t want to use can be filled by the use of “NULL” and environment variables can be included in file paths to make this as flexible as possible.

Option Explicit

CreateShortcut "%UserProfile%\SendTo\Test Link.lnk", _
 "C:\Scripts\Test-Script.vbs", NULL, "A test script", NULL, NULL, NULL, NULL, NULL

Function CreateShortcut(p_strShortcutPath, p_strTargetPath, _
 p_strArguments, p_strComment, _
 p_strHotKey, p_strIconPath, p_strIconPosition, _
 p_strWindowStyle, p_strWorkingDirectory)

 'Example Parameters
 'p_strShortcutPath = "%UserProfile%\SendTo\Shortcut to Something"
 'p_strTargetPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strArguments = "/O"
 'p_strDescription = "Start Something"
 'p_strHotKey = "CTRL+ALT+Q"
 'p_strIconPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strIconPosition = "3"
 'p_strWindowStyle = "1"
 'p_strWorkingDirectory = "C:\Working\"
 'Use NULL for parameters which are not to be set

 Dim objShell, objShortcut

 Set objShell = WScript.CreateObject("WScript.Shell")
 Set objShortcut = objShell.CreateShortcut(objShell.ExpandEnvironmentStrings(p_strShortcutPath))

 If Not isNull(p_strTargetPath) Then
 objShortcut.TargetPath = objShell.ExpandEnvironmentStrings(p_strTargetPath)
 End If

 If Not isNull(p_strArguments) Then
 objShortcut.Arguments = p_strArguments
 End If

 If Not isNull(p_strComment) Then
 objShortcut.Description = p_strComment
 End If

 If Not isNull(p_strHotKey) Then
 objShortcut.HotKey = p_strHotKey
 End If

 If Not isNull(p_strIconPath) Then
 objShortcut.IconLocation = objShell.ExpandEnvironmentStrings(p_strIconPath & ", " & p_strIconPosition)
 End If

 If Not isNull(p_strWindowStyle) Then
 objShortcut.WindowStyle = p_strWindowStyle
 End If

 If Not isNull(p_strWorkingDirectory) Then
 objShortcut.WorkingDirectory = objShell.ExpandEnvironmentStrings(p_strWorkingDirectory)
 End If

End Function