Results 1 to 2 of 2

Thread: VB Snippet - Create Desktop Shortcut

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet - Create Desktop Shortcut

    There are much shorter ways, but,

    VB Code:
    1. 'Module Code
    2. Public Enum CSIDL_FOLDERS
    3.     CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
    4.     CSIDL_PROGRAMS = 2 '// Program Files
    5.     CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
    6.     CSIDL_PRINTERS = 4 '// Printers - virtual folder
    7.     CSIDL_DOCUMENTS = 5 '// My Documents
    8.     CSIDL_FAVORITES = 6 '// Favourites
    9.     CSIDL_STARTUP = 7 '// Startup Folder
    10.     CSIDL_RECENT = 8 '// Recent Documents
    11.     CSIDL_SENDTO = 9 '// Send To Folder
    12.     CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
    13.     CSIDL_STARTMENU = 11 '// Start Menu
    14.     CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
    15.     CSIDL_DRIVES = 17 '// My Computer - virtual folder
    16.     CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
    17.     CSIDL_NETHOOD = 19 '// NetHood Folder
    18.     CSIDL_FONTS = 20 '// Fonts folder
    19.     CSIDL_SHELLNEW = 21 '// ShellNew folder
    20. End Enum
    21. Private Const FO_MOVE = &H1
    22. Private Const FO_RENAME = &H4
    23. Private Const FOF_SILENT = &H4
    24. Private Const FOF_NOCONFIRMATION = &H10
    25. Private Const FOF_RENAMEONCOLLISION = &H8
    26. Private Const MAX_PATH As Integer = 260
    27. Private Const SHARD_PATH = &H2&
    28. Private Const SHCNF_IDLIST = &H0
    29. Private Const SHCNE_ALLEVENTS = &H7FFFFFFF
    30.  
    31. Private Type SHFILEOPSTRUCT
    32.     hwnd   As Long
    33.     wFunc As Long
    34.     pFrom As String
    35.     pTo     As String
    36.     fFlags  As Integer
    37.     fAborted       As Boolean
    38.     hNameMaps As Long
    39.     sProgress      As String
    40. End Type
    41.  
    42. Private Type ****EMID
    43.     cb As Long
    44.     abID As Byte
    45. End Type
    46.  
    47. Private Type ITEMIDLIST
    48.     mkid As ****EMID
    49. End Type
    50.  
    51. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    52. Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
    53.         (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    54. Private Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _
    55.         "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    56.         ByRef ppidl As Long) As Long
    57. Private Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _
    58.         ByVal dwdata As String) As Long
    59. Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
    60.         (lpFileOp As SHFILEOPSTRUCT) As Long
    61. Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _
    62.         ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
    63. Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
    64.         (ByVal pidl As Long, ByVal pszPath As String) As Long
    65.  
    66.  
    67. Private Function fGetSpecialFolder(CSIDL As Long) As String
    68. Dim sPath As String
    69. Dim IDL As ITEMIDLIST
    70. '
    71. ' Retrieve info about system folders such as the "Recent Documents" folder.
    72. ' Info is stored in the IDL structure.
    73. '
    74. fGetSpecialFolder = ""
    75. If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
    76.     '
    77.     ' Get the path from the ID list, and return the folder.
    78.     '
    79.     sPath = Space$(MAX_PATH)
    80.     If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
    81.         fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
    82.     End If
    83. End If
    84. End Function
    85.  
    86. 'Thanks to Mike J for pointing out some errors
    87. 'and improving this routine
    88.  
    89. 'Module Code
    90. Public Function CreateShortcut(ByRef txtFilePath As String, _
    91.         ByRef txtName As String, ByRef vTarget As CSIDL_FOLDERS)
    92.     Dim I As Integer
    93.     Dim lResult As Long
    94.     Dim lpil As Long
    95.     Dim sFilePath As String
    96.     Dim sFileName As String
    97.     Dim sRecentPath As String
    98.     Dim sDesktopPath As String
    99.     Dim sFilePathOld As String
    100.     Dim sFilePathNew As String
    101.     Dim sShortCutName As String
    102.     Dim SMsg As String
    103.     Dim SHFileOp As SHFILEOPSTRUCT
    104.  
    105.     ' Add a shortcut to any path virtual folder.
    106.     '
    107.     ' Get the .exe path and display name associated with the
    108.     ' button that was right clicked (determined by ptbrRightButton).
    109.     '
    110.     On Error GoTo cmdCreateError
    111.     Screen.MousePointer = vbHourglass
    112.     sFilePath = Trim$(txtFilePath)
    113.     sShortCutName = Trim$(txtName) & ".lnk"
    114.     '
    115.     ' Get the paths of the folders to add the shortcuts to.
    116.     ' The folders are the Recent Files List and the Desktop.
    117.     '
    118.     sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    119.     '
    120.     ' NOTE: to create the shortcut in another folder, set sDesktopPath
    121.     ' to that folder.
    122.     sDesktopPath = fGetSpecialFolder(VTarget)
    123.     SMsg = "Error retrieving folder location."
    124.     If sRecentPath <> "" And sDesktopPath <> "" Then
    125.         '
    126.         ' Create a shortcut in the Recent Files list.
    127.         '
    128.         sMsg = "Error adding shortcut to the Recent File list."
    129.         lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    130.         Call Sleep(1500)
    131.         If lResult Then
    132.    
    133.             ' Extract the .exe name from the path.
    134.             I = 1
    135.             sFileName = sFilePath
    136.             Do While I
    137.                 I = InStr(1, sFileName, "\")
    138.                 If I Then sFileName = Mid$(sFileName, I + 1)
    139.             Loop
    140.            
    141.            
    142.             ' Move the shortcut from the Recent folder to the Desktop.
    143.             ' Since the shortcut now resides in the Recent folder,
    144.             ' modify the file name to include the Recent folder
    145.             ' path. Also, append ".lnk" to the original filename.
    146.             '
    147.             sFilePath = sRecentPath & "\" & sFileName & ".lnk" & _                vbNullChar & vbNullChar
    148.            
    149.             With SHFileOp
    150.                 .wFunc = FO_MOVE
    151.                 .pFrom = sFilePath
    152.                 .pTo = sDesktopPath
    153.                 .fFlags = FOF_SILENT
    154.             End With
    155.            
    156.             SMsg = "Error creating desktop shortcut."
    157.             lResult = SHFileOperation(SHFileOp)
    158.             Sleep (1500)
    159.             If lResult = 0 Then
    160.            
    161.                 '
    162.                 ' Rename the link.
    163.                
    164.                 sFilePathOld = sDesktopPath & "\" & sFileName & ".lnk" & _
    165.                      vbNullChar & vbNullChar
    166.                 sFilePathNew = sDesktopPath & "\" & sShortCutName & _
    167.                      vbNullChar & vbNullChar
    168.                 With SHFileOp
    169.                   .wFunc = FO_RENAME
    170.                   .pFrom = sFilePathOld
    171.                   .pTo = sFilePathNew
    172.                   .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    173.                 End With
    174.                 SMsg = "Error renaming desktop shortcut."
    175.                 lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    176.                 SMsg = ""
    177.                 '
    178.                 ' Refresh the desktop to display the shortcut.
    179.                 '
    180.                 Call SHGetSpecialFolderLocationD(0, CSIDL_DESKTOP, lpil)
    181.                
    182.                 Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    183.                
    184.             End If
    185.         End If
    186.     End If
    187.     Screen.MousePointer = vbDefault
    188.     Exit Function
    189.    
    190. cmdCreateError:
    191.     MsgBox "Error creating desktop shortcut. " & SMsg, vbExclamation, "Create Shortcut"
    192. End Function
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    New Member Bayu Malmsteen's Avatar
    Join Date
    Apr 2010
    Location
    Bandung, Indonesia
    Posts
    14

    Re: VB Snippet - Create Desktop Shortcut

    Quote Originally Posted by James Stanich View Post
    There are much shorter ways, but,

    VB Code:
    1. 'Module Code
    2. Public Enum CSIDL_FOLDERS
    3.     CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
    4.     CSIDL_PROGRAMS = 2 '// Program Files
    5.     CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
    6.     CSIDL_PRINTERS = 4 '// Printers - virtual folder
    7.     CSIDL_DOCUMENTS = 5 '// My Documents
    8.     CSIDL_FAVORITES = 6 '// Favourites
    9.     CSIDL_STARTUP = 7 '// Startup Folder
    10.     CSIDL_RECENT = 8 '// Recent Documents
    11.     CSIDL_SENDTO = 9 '// Send To Folder
    12.     CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
    13.     CSIDL_STARTMENU = 11 '// Start Menu
    14.     CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
    15.     CSIDL_DRIVES = 17 '// My Computer - virtual folder
    16.     CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
    17.     CSIDL_NETHOOD = 19 '// NetHood Folder
    18.     CSIDL_FONTS = 20 '// Fonts folder
    19.     CSIDL_SHELLNEW = 21 '// ShellNew folder
    20. End Enum
    21. Private Const FO_MOVE = &H1
    22. Private Const FO_RENAME = &H4
    23. Private Const FOF_SILENT = &H4
    24. Private Const FOF_NOCONFIRMATION = &H10
    25. Private Const FOF_RENAMEONCOLLISION = &H8
    26. Private Const MAX_PATH As Integer = 260
    27. Private Const SHARD_PATH = &H2&
    28. Private Const SHCNF_IDLIST = &H0
    29. Private Const SHCNE_ALLEVENTS = &H7FFFFFFF
    30.  
    31. Private Type SHFILEOPSTRUCT
    32.     hwnd   As Long
    33.     wFunc As Long
    34.     pFrom As String
    35.     pTo     As String
    36.     fFlags  As Integer
    37.     fAborted       As Boolean
    38.     hNameMaps As Long
    39.     sProgress      As String
    40. End Type
    41.  
    42. Private Type ****EMID
    43.     cb As Long
    44.     abID As Byte
    45. End Type
    46.  
    47. Private Type ITEMIDLIST
    48.     mkid As ****EMID
    49. End Type
    50.  
    51. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    52. Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
    53.         (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    54. Private Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _
    55.         "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    56.         ByRef ppidl As Long) As Long
    57. Private Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _
    58.         ByVal dwdata As String) As Long
    59. Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
    60.         (lpFileOp As SHFILEOPSTRUCT) As Long
    61. Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _
    62.         ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
    63. Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
    64.         (ByVal pidl As Long, ByVal pszPath As String) As Long
    65.  
    66.  
    67. Private Function fGetSpecialFolder(CSIDL As Long) As String
    68. Dim sPath As String
    69. Dim IDL As ITEMIDLIST
    70. '
    71. ' Retrieve info about system folders such as the "Recent Documents" folder.
    72. ' Info is stored in the IDL structure.
    73. '
    74. fGetSpecialFolder = ""
    75. If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
    76.     '
    77.     ' Get the path from the ID list, and return the folder.
    78.     '
    79.     sPath = Space$(MAX_PATH)
    80.     If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
    81.         fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
    82.     End If
    83. End If
    84. End Function
    85.  
    86. 'Thanks to Mike J for pointing out some errors
    87. 'and improving this routine
    88.  
    89. 'Module Code
    90. Public Function CreateShortcut(ByRef txtFilePath As String, _
    91.         ByRef txtName As String, ByRef vTarget As CSIDL_FOLDERS)
    92.     Dim I As Integer
    93.     Dim lResult As Long
    94.     Dim lpil As Long
    95.     Dim sFilePath As String
    96.     Dim sFileName As String
    97.     Dim sRecentPath As String
    98.     Dim sDesktopPath As String
    99.     Dim sFilePathOld As String
    100.     Dim sFilePathNew As String
    101.     Dim sShortCutName As String
    102.     Dim SMsg As String
    103.     Dim SHFileOp As SHFILEOPSTRUCT
    104.  
    105.     ' Add a shortcut to any path virtual folder.
    106.     '
    107.     ' Get the .exe path and display name associated with the
    108.     ' button that was right clicked (determined by ptbrRightButton).
    109.     '
    110.     On Error GoTo cmdCreateError
    111.     Screen.MousePointer = vbHourglass
    112.     sFilePath = Trim$(txtFilePath)
    113.     sShortCutName = Trim$(txtName) & ".lnk"
    114.     '
    115.     ' Get the paths of the folders to add the shortcuts to.
    116.     ' The folders are the Recent Files List and the Desktop.
    117.     '
    118.     sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    119.     '
    120.     ' NOTE: to create the shortcut in another folder, set sDesktopPath
    121.     ' to that folder.
    122.     sDesktopPath = fGetSpecialFolder(VTarget)
    123.     SMsg = "Error retrieving folder location."
    124.     If sRecentPath <> "" And sDesktopPath <> "" Then
    125.         '
    126.         ' Create a shortcut in the Recent Files list.
    127.         '
    128.         sMsg = "Error adding shortcut to the Recent File list."
    129.         lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    130.         Call Sleep(1500)
    131.         If lResult Then
    132.    
    133.             ' Extract the .exe name from the path.
    134.             I = 1
    135.             sFileName = sFilePath
    136.             Do While I
    137.                 I = InStr(1, sFileName, "\")
    138.                 If I Then sFileName = Mid$(sFileName, I + 1)
    139.             Loop
    140.            
    141.            
    142.             ' Move the shortcut from the Recent folder to the Desktop.
    143.             ' Since the shortcut now resides in the Recent folder,
    144.             ' modify the file name to include the Recent folder
    145.             ' path. Also, append ".lnk" to the original filename.
    146.             '
    147.             sFilePath = sRecentPath & "\" & sFileName & ".lnk" & _                vbNullChar & vbNullChar
    148.            
    149.             With SHFileOp
    150.                 .wFunc = FO_MOVE
    151.                 .pFrom = sFilePath
    152.                 .pTo = sDesktopPath
    153.                 .fFlags = FOF_SILENT
    154.             End With
    155.            
    156.             SMsg = "Error creating desktop shortcut."
    157.             lResult = SHFileOperation(SHFileOp)
    158.             Sleep (1500)
    159.             If lResult = 0 Then
    160.            
    161.                 '
    162.                 ' Rename the link.
    163.                
    164.                 sFilePathOld = sDesktopPath & "\" & sFileName & ".lnk" & _
    165.                      vbNullChar & vbNullChar
    166.                 sFilePathNew = sDesktopPath & "\" & sShortCutName & _
    167.                      vbNullChar & vbNullChar
    168.                 With SHFileOp
    169.                   .wFunc = FO_RENAME
    170.                   .pFrom = sFilePathOld
    171.                   .pTo = sFilePathNew
    172.                   .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    173.                 End With
    174.                 SMsg = "Error renaming desktop shortcut."
    175.                 lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    176.                 SMsg = ""
    177.                 '
    178.                 ' Refresh the desktop to display the shortcut.
    179.                 '
    180.                 Call SHGetSpecialFolderLocationD(0, CSIDL_DESKTOP, lpil)
    181.                
    182.                 Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    183.                
    184.             End If
    185.         End If
    186.     End If
    187.     Screen.MousePointer = vbDefault
    188.     Exit Function
    189.    
    190. cmdCreateError:
    191.     MsgBox "Error creating desktop shortcut. " & SMsg, vbExclamation, "Create Shortcut"
    192. End Function
    your script doesn't work as well... can u explain how does it work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width