You will need to include the following reference into the project:
Windows Script Host Object Model
Sub fCreateShortcutOnDesktop()
Dim WSHShell As IWshRuntimeLibrary.IWshShell_Class
Dim WSHShortcut As IWshRuntimeLibrary.IWshShortcut_Class
Dim strFullFilePathName As String
Dim strDesktopPath As String
Dim strFileName As String
Dim strPath As String
' Create a Windows Shell Object
Set WSHShell = New IWshRuntimeLibrary.IWshShell_Class
' Read desktop path using WshSpecialFolders object
strDesktopPath = WSHShell.SpecialFolders.Item("Desktop"
' Create a shortcut object on the desktop
Set WSHShortcut = WSHShell.CreateShortcut(strDesktopPath & "\" & strFileName & ".lnk"
' Set shortcut object properties and save it
With WSHShortcut
.TargetPath = WSHShell.ExpandEnvironmentStrings(strFullFilePathName)
.WorkingDirectory = WSHShell.ExpandEnvironmentStrings(strPath)
.WindowStyle = 4
.Save
End With
fCreateShortcutOnDesktop = 1
Set WSHShell = Nothing
End Sub
Good Luck
-------------- As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
I have been looking to do this also. I dont know why it has to be this hard. My question is, Where do you call the sub from? and whats the file name for the windows script host object module?
You will find the Windows Script Host Object Model near the end of the Project References.
I have updated the code using two parameters. The first parameter is the directory name, and the second is the name of the executable file. I would drop this code into a module.
Public Sub CreateDeskTopIcon(rStr_DirName As String, rStr_ExeName As String)
Dim lCls_WSHShell As IWshRuntimeLibrary.IWshShell_Class
Dim lCls_WSHShortcut As IWshRuntimeLibrary.IWshShortcut_Class
Dim lStr_FullPathName As String
Dim lStr_DesktopPath As String
Dim lStr_DeskTopIcon As String
rStr_DirName = Trim(rStr_DirName)
If (Right(rStr_DirName, 1) <> "\" Then
rStr_DirName = rStr_DirName & "\"
End If
rStr_ExeName = Trim(rStr_ExeName)
If (UCase(Right(rStr_ExeName, 4)) = ".EXE" Then
rStr_ExeName = Left(rStr_ExeName, (Len(rStr_ExeName) - 4))
End If
lStr_FullPathName = rStr_DirName + rStr_ExeName
Set lCls_WSHShell = New IWshRuntimeLibrary.IWshShell_Class
lStr_DesktopPath = lCls_WSHShell.SpecialFolders.Item("Desktop"
lStr_DeskTopIcon = lStr_DesktopPath & "\" & rStr_ExeName & ".lnk"
Set lCls_WSHShortcut = lCls_WSHShell.CreateShortcut(lStr_DeskTopIcon)
With lCls_WSHShortcut
.TargetPath = lCls_WSHShell.ExpandEnvironmentStrings(lStr_FullPathName)
.WorkingDirectory = lCls_WSHShell.ExpandEnvironmentStrings(rStr_DirName)
.WindowStyle = 4
.Save
End With
Set lCls_WSHShortcut = Nothing
Set lCls_WSHShell = Nothing
End Sub
'===========================================
I tested it by placing a button on a form, and what follows is the button click event.
I did notice that when I looked at the properties of the icon the .EXE was not on the end of the shortcut. do you think that will cause problems if the icon trys to execute a packaged app?? Also how do you add an icon to the shortcut?
Here's an example of a slightly shorter variant of cvajun's solution. Create a form with a command button, and drop in the following code: [tt]
Option Explicit
Private Sub Command1_Click()
CreateMyShortcut "c:\test.txt", "test.lnk"
End Sub [/tt]
Now create a module, and drop in the following (note that the SpecialFolders index can actually be any of the following: AllUsersDesktop, AllUsersStartMenu, AllUsersPrograms, AllUsersStartup, Desktop, Favorites, Fonts, MyDocuments, NetHood, PrintHood, Programs, Recent, SendTo, StartMenu, Startup, Templates) [tt]
Option Explicit
Public Sub CreateMyShortcut(strTarget As String, strShortCutName As String, Optional strStartIn As String = "", Optional strArguments As String = ""
Dim wsh As Object
Dim sf As Object
Dim Shortcut As Object '
Set wsh = CreateObject("wscript.shell"
Set sf = wsh.SpecialFolders
I don't know about it working on all of the other OS' but it works on my XP.
Declare Function fCreateShellLink Lib "VB5STKIT.DLL" _
(ByVal lpstrFolderName As String, ByVal lpstrLinkName _
As String, ByVal lpstrLinkPath As String, ByVal _
lpstrLinkArgs As String) As Long
'create the shortcut on the start menu
lngresult = fCreateShellLink("..", "Link to my", "Path to my program", ""
'create the shortcut on the desktop
lngresult = fCreateShellLink("..\..\desktop", "Link to my program", "Path to my program", ""
ts2032,
The basic difference is that to use the fCreateShellLink API is that you have to exactly where the desktop directory is. In your example, you use a relative pathname. If the user were to move to the exe up or down one diretory level, or perhaps chooses to install it on a different drive, your program would encounter some difficulties.
And depending on to whom you have distributed this program, the desktop directory may be c:\windows\desktop, or maybe c:\winnt\desktop, or perhaps something else, so putting in an absolute pathername doesn't guarantee success either.
Good Luck
-------------- As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.