?
Solved

Using vb6 how to add a shortcut of an exe file to the desktop

Posted on 2010-03-28
8
Medium Priority
?
2,547 Views
Last Modified: 2012-08-13
I have windows 7,  using vb6,   how to add a shortcut of an executable file, to the desktop of windows 7?
0
Comment
Question by:saljas
  • 5
  • 2
8 Comments
 

Expert Comment

by:Swordsower
ID: 28857227
Okay, so this is what I've found that should be able to do what you need and more.
'System & API - How to Add a shortcut in current users Desktop, Programs menu and Startup folder
Private Declare Function fCreateShellLink Lib "vb6stkit.dll" (ByVal lpstrFolderName As String, _
    ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArguments As String, _
    ByVal fPrivate As Long, ByVal sParent As String) As Long

Private Sub Form_Load()
    Dim strGroupName As String, strLinkName As String
    Dim strLinkPath As String, strLinkArguments As String, sParent As String
    Dim fPrivate As Boolean, fSuccess As Boolean
    strLinkName = "Shortcut to Calculator"
    strLinkPath = "c:\Windows\calc.exe"
    strLinkArguments = ""
    fPrivate = True
    'Add shortcut to desktop.
    strGroupName = "..\..\Desktop"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created desktop shortcut"
    Else
        MsgBox "Unable to create desktop shortcut"
    End If
   'Add shortcut to Programs menu.
    strGroupName = "$(Programs)"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created shortcut on Programs menu"
    Else
        MsgBox "Unable to create shortcut on Programs menu"
    End If
    ' Add shortcut to Startup folder of Programs menu.
    strGroupName = "Startup"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created shortcut in Startup folder"
    Else
        MsgBox "Unable to create shortcut in Startup folder"
    End If
End Sub
 
Of course, this is just an example.  The meat and potatoes of this code is in bold.  This code will simply create a shortcut in three places, Desktop, Programs Menu, and Startup Folder.  My advice: Play with the code until you get it to do exactly what you want.  Should take no more than half an hour for even a beginner.
Any questions, email me (Xefanro@yahoo.com).
0
 

Author Comment

by:saljas
ID: 28861790
many thanks Swordsower
Your code did not work.    We are working on windows 7 64bit.   It adds to program menu, it adds to the startup, but it does not add to the desktop.  I think your code will work on 32 bit only.
thanks any way.
0
 

Author Comment

by:saljas
ID: 28861922
Mr Swordsower
I have just tried it on win7 32bit, it also did not work.  So it seems the problam is only with win 7.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Assisted Solution

by:Swordsower
Swordsower earned 200 total points
ID: 28867097
Got it, it's the path the code is using for the desktop.  It currently says strGroupName = "..\..\Desktop".  The ...\...\ doesn't mean anything, it's just a placeholder.  It should read something like this "C:\Documents and Settings\User Name\Desktop".
But, instead of doing that, let's make the code more robust, shall we?  Make it say this:
strGroupName = getPathToDesktop
Now, find someplace convenient, and add this function.
Public Function getPathToDesktop() As String
    Dim WSHShell As Object
    Set WSHShell = CreateObject("WScript.Shell")
    If (Not WSHShell Is Nothing) Then
        getPathToDesktop = WSHShell.SpecialFolders("Desktop")
    End If
    Set WSHShell = Nothing
End Function

Trust me, this is hundreds of times better, because now it finds the desktop independent of the specific user's username.  If you had specified a Username in the string, your code would only work for that user.  Doing it this way lets any user run the code and it will 'find' the desktop regardless.  I know this function works in XP, but I see no reason why it wouldn't work in 7.
-------Unrelated Notes Below This Line-------
One other caveat: This has nothing to do with your 64 bit version.  Do not assume that the 64 bit system runs the code any different - VB* was designed by microsoft, it is VERY rare that a microsoft OS is going to have a problem with the bit level.  Be mindful of the fact that the path to (for example) your 'my music' folder in XP and earlier looks like this "C:\Documents and Settings\User Name\My Documents\My Music\" and in Vista and XP it looks like this "C:\Documents and Settings\User Name\Music".  Invalid path is more common than OS conflicts (at least in VB*).
*I use VB6 and VB.NET interchangeably in this instance, as they are both Microsoft, and therefore marry up with what I am saying in the paragraph.
0
 

Author Comment

by:saljas
ID: 28879025
By replacing
strGroupName = "..\..\Desktop"   with  strGroupName = getPathToDesktop
and adding the Public Function it can get the desktop of the current user FINE.  In fact it is helfull for future use.
But
it did NOT add to desktop in all windows   xp  2000   7  (fSuccess = False)
and by returning back to   strGroupName = "..\..\Desktop"
xp   2000    work fine  (fSuccess = True)
BUT with    win 7   still did not work  (fSuccess = False)
0
 
LVL 5

Accepted Solution

by:
Leithauser earned 800 total points
ID: 28887355
Use this function to create an icon on the desktop. AppName$ is the name to display under the icon,FinalPath$ is the folder name ending in \, and ExeName$ is the exe file name.

Function CreateIcon(AppName$, FinalPath$, ExeName$) As Integer

'AppName$ is name to display on icon
'FinalPath$ is path of program folder
' ExeName$ is file name
On Error Resume Next

Dim wshShell As Object
Set wshShell = CreateObject("WScript.Shell")
Dim lnkDesktop As Object
Set lnkDesktop = wshShell.CreateShortcut(wshShell.SpecialFolders("Desktop") & "\" & AppName$ & ".lnk")
With lnkDesktop
    .Description = AppName$ ' "My Application"
    '.TargetPath = FinalPath$ & "\" & ExeName$ ' "C:\Program Files\My Application\MyApp.exe"
    .TargetPath = FinalPath$ & ExeName$ ' Note: Removed \ to conform to format used in other subs
    .WorkingDirectory = FinalPath$ ' "C:\Program Files\My Application"
    .Save
End With
Set lnkDesktop = Nothing
Set wshShell = Nothing


'    Dim wshShell As Object
'    Set wshShell = CreateObject("WScript.Shell")
'    Dim lnkDesktop As Object
'    Set lnkDesktop = wshShell.CreateShortcut(wshShell.SpecialFolders("Desktop") & "\My Application.lnk")
'    With lnkDesktop
'         .Description = "My Application"
'         .TargetPath = "C:\Program Files\My Application\MyApp.exe"
'         .WorkingDirectory = "C:\Program Files\My Application"
'         .Save
'    End With
'       Set lnkDesktop = Nothing
'       Set wshShell = Nothing

If Err Then
    Err = 0
    CreateIcon = False
Else
    CreateIcon = True
End If

End Function

0
 

Author Closing Comment

by:saljas
ID: 31707995
Many thanks for Swordsower for providing with a very good way of getting the desktop.
Many thanks for  Leithauser for the function that is able to add an icon to all windows platforms.
0
 

Author Comment

by:saljas
ID: 28991137
To  Leithauser
Yous function works fine.  Many thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question