Your Ad Here

November 14, 2007

Add Shortcuts To The Users Desktop

Author:Michael Sanchez
Category:Windows 2000/XP
Type:Snippets
Difficulty:Beginning

Version Compatibility:VB Script/HTML

More information: You can use these WSH (Windows Script Host) functions to add a shortcut to a specified file to the user's desktop. This example creates a shortcut to Windows Calculator, but alternatively, you could wrap this script into a function that accepts arguments for path, file and even icon types. Just loop the WSH arguments collection to get your values. You can also add shortcuts to other Windows special folders using the .specialFolders function of the WSH shell object.

This code has been viewed 41091 times.

Instructions: Copy the declarations and code below and paste directly into your VB project


Code


'##############################################################
'# CREATESHORTCUT_WSH.VBS
'# CREATES A SHORTCUT ON THE USERS DESKTOP TO A SPECIFIED FILE
'#
'# USES: WINDOWS SCRIPT HOST
'# LANGUAGE: VISUAL BASIC SCRIPTING EDITION
'##############################################################

'FORCE EXPLICIT VARIABLE DECLARATION
option explicit

'STEP OVER ERRORS FOR CUSTOM ERROR REPORTING
on error resume next

'DECLARE VARIABLES
dim shell, desktopPath, link, sys32Path

'INSTANTIATE THE WINDOWS SCRIPT HOST SHELL OBJECT
Set shell = WScript.CreateObject("WScript.shell")

'SET THE PATH TO THE WINDOWS DESKTOP FOLDER & MY DOCUMENTS FOLDER
desktopPath = shell.SpecialFolders("Desktop")
sys32Path = "%SystemRoot%\system32"

'CREATE A SHORTCUT ON THE USER'S DESKTOP
Set link = shell.CreateShortcut(desktopPath & "\shortcut to Calculator.lnk")

'SET THE PROPERTIES FOR THE SHORTCUT
link.Description = "My Shortcut"
link.TargetPath = sys32Path & "\calc.exe"
link.WindowStyle = 3
link.WorkingDirectory = desktopPath
link.Save

'CLEANUP OBJECTS
set shell = nothing

'LET THE USER KNOW IF THERE WAS AN ERROR AND WHAT IT WAS
'OTHERWISE CONFIRM SHORCUT CREATION
if err.number <> 0 then
msgbox "There was an error creating your shortcut." & vbCrLf & err.description & vbCrLf & err.source, vbOKOnly-vbExclamation, "Shortcut Builder"
else
msgBox "Your new shortcut has been created!" & vbCrLf & "Please check your Windows Desktop.", vbOKOnly-vbInformation, "Shortcut Builder"
end if


No comments: