Difference between revisions of "Create a shortcut"

From HashVB
Jump to: navigation, search
(Code sample)
 
(Expanded article)
Line 1: Line 1:
  Public Sub CreateShortcut(Destination As String, Name As String, _
+
Creating shortcuts in VB is relatively easy with help of the Windows Scripting Shell object. This allows you to create a shortcut at a given path and then set all the properties as you need.
   Target As String, Parameters As String)
+
 
 +
  Public Sub CreateShortcut(ByVal Path As String, ByVal Name As String, _
 +
   ByVal Target As String, Optional ByVal Parameters As String = "")
 
  Dim Shell As Object 'WshShell
 
  Dim Shell As Object 'WshShell
  Dim NewShortcut As Object 'WshShortcut
+
  Dim Shortcut As Object 'WshShortcut
       
+
 
   Set Shell = CreateObject("wscript.shell.1")
 
   Set Shell = CreateObject("wscript.shell.1")
   Set NewShortcut = Shell.CreateShortcut(Destination & Name & ".lnk")
+
   Set Shortcut = Shell.CreateShortcut(Path & Name & ".lnk")
   NewShortcut.TargetPath = Target
+
   Shortcut.TargetPath = Target
   NewShortcut.Arguments = Parameters
+
   Shortcut.Arguments = Parameters
   NewShortcut.Save
+
   Shortcut.Save
 
  End Sub
 
  End Sub
 +
 +
The variables are defined as "Object" because we use late binding, this means you do not need to add a reference and so it is not tied to a specific version of the Windows Scripting Host.
 +
 +
Please note that you can not include command line parameters in the TargetPath property.
 +
 +
=== See Also ===
 +
[http://msdn.microsoft.com/library/en-us/script56/html/5ce04e4b-871a-4378-a192-caa644bd9c55.asp WshShortcut object definition on MSDN]

Revision as of 11:44, 5 January 2006

Creating shortcuts in VB is relatively easy with help of the Windows Scripting Shell object. This allows you to create a shortcut at a given path and then set all the properties as you need.

Public Sub CreateShortcut(ByVal Path As String, ByVal Name As String, _
  ByVal Target As String, Optional ByVal Parameters As String = "")
Dim Shell As Object 'WshShell
Dim Shortcut As Object 'WshShortcut

  Set Shell = CreateObject("wscript.shell.1")
  Set Shortcut = Shell.CreateShortcut(Path & Name & ".lnk")
  Shortcut.TargetPath = Target
  Shortcut.Arguments = Parameters
  Shortcut.Save
End Sub

The variables are defined as "Object" because we use late binding, this means you do not need to add a reference and so it is not tied to a specific version of the Windows Scripting Host.

Please note that you can not include command line parameters in the TargetPath property.

See Also

WshShortcut object definition on MSDN