Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a shortcut

Status
Not open for further replies.

devinci

MIS
Feb 15, 2002
46
CA
Hi...I need to be able to create a shortcut for an executable file on the desktop and in the start menu.

I need to be able to create it on both windows NT, 2000 and windows 95, 98 and ME.

Thanks for helping...
 
You could try the following:


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

' Set the file's name and path...

strPath = "C:\vb_projs\LineCount\"
strFileName = "LineCount" ' Note without .exe
strFullFilePathName = strPath + strFileName

' 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


 
Thanks Cajun...

It works fine for the shortcut on the desktop but I don't have the one in the start up menu...

I had a compile error for "fCreateShortcutOnDesktop = 1"

Expected Function or variable.

One more thing will this work on all different platform for windows.

Thanks for all your help...
 
Ok...I was able to create a shortcut on the start menu...

But...I would still like to know if the code will work no matter which version of Windows is installed on the computer...

Thanks...
 
Thread delay. Cajun has essentially given the solution I was thinking of.
 
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?

What would be the best way to use the code?

Thanks
 
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) <> &quot;\&quot;) Then
rStr_DirName = rStr_DirName & &quot;\&quot;
End If
rStr_ExeName = Trim(rStr_ExeName)
If (UCase(Right(rStr_ExeName, 4)) = &quot;.EXE&quot;) 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(&quot;Desktop&quot;)
lStr_DeskTopIcon = lStr_DesktopPath & &quot;\&quot; & rStr_ExeName & &quot;.lnk&quot;
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.

Private Sub cmdDesktop_Click()

CreateDeskTopIcon &quot;c:\vb_projs\LineCount\&quot;, &quot;LineCount&quot;

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Worked perfect thank you!

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?

Thanks
 
>why it has to be this hard

Not all that long ago it was a darn sight harder.

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 &quot;c:\test.txt&quot;, &quot;test.lnk&quot;
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 = &quot;&quot;, Optional strArguments As String = &quot;&quot;)
Dim wsh As Object
Dim sf As Object
Dim Shortcut As Object '

Set wsh = CreateObject(&quot;wscript.shell&quot;)
Set sf = wsh.SpecialFolders

Set Shortcut = wsh.CreateShortcut(sf(&quot;AllUsersDesktop&quot;) & &quot;\&quot; & strShortCutName)
Shortcut.TargetPath = strTarget
Shortcut.WorkingDirectory = strStartIn
Shortcut.Arguments = strArguments
Shortcut.Save

Set Shortcut = wsh.CreateShortcut(sf(&quot;AllUsersStartup&quot;) & &quot;\&quot; & strShortCutName)
Shortcut.TargetPath = strTarget
Shortcut.WorkingDirectory = strStartIn
Shortcut.Arguments = strArguments
Shortcut.Save

End Sub
 
Guys,

I don't know about it working on all of the other OS' but it works on my XP.

Declare Function fCreateShellLink Lib &quot;VB5STKIT.DLL&quot; _
(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(&quot;..&quot;, &quot;Link to my&quot;, &quot;Path to my program&quot;, &quot;&quot;)


'create the shortcut on the desktop
lngresult = fCreateShellLink(&quot;..\..\desktop&quot;, &quot;Link to my program&quot;, &quot;Path to my program&quot;, &quot;&quot;)

clue me in on the shortcomings of this method.

tsmith
 
If I want to uninstall the executable file...how would I search to see if any shortcut of that file exist to be able to delete them...

And will the code for the create a shortcut works on a French OS.

Thank...
 
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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top