CodeItBetter Programming Another VB Programming Blog

How to Add a shortcut in current users Desktop, Programs menu and Startup folder

Posted on June 23, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'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
Comments (1) Trackbacks (0)
  1. Well, when I try to run it it says, Comments only allowed after End Dub, etc. this is the code I use:

    Private Sub Command1_Click()
    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 = Text3.Text
    strLinkPath = Text1.Text
    strLinkArguments = “”
    fPrivate = True
    ‘Add shortcut to the path selected in the second TextBox
    strGroupName = Text2.Text
    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, Loaction: ” & strGroupName
    Else
    MsgBox “Unable to create shortcut. (File path: ” & strLinkPath & ” Shortcut Path: ” & strGroupName & “)”
    End If
    End Sub

    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
    End Function

    Can you help me?


Leave a comment


 

No trackbacks yet.