Members
Technology Zones
Articles
Hosted By
Info
|
Rated
Read 76,993 times
Related Categories
Create a shortcut
Creating a shortcut in VB is far more complicated than it should be. The easiest
way to create a shortcut is to add it to the recent documents list, and then
move it to where you want it to go.
To use the code below, simply call
Call CreateShortcut(strFilePath, strName, CSIDL_FOLDER)
Where strFilePath is the filename that the shortcut should point to, strName
is the name of the shortcut, and CSIDL_FOLDER is one of the
constants in the enum CSIDL_FOLDERS
Public Enum CSIDL_FOLDERS CSIDL_DESKTOP = &H0 CSIDL_PROGRAMS = 2 CSIDL_CONTROLS = 3 CSIDL_PRINTERS = 4 CSIDL_DOCUMENTS = 5 CSIDL_FAVORITES = 6 CSIDL_STARTUP = 7 CSIDL_RECENT = 8 CSIDL_SENDTO = 9 CSIDL_BITBUCKET = 10 CSIDL_STARTMENU = 11 CSIDL_DESKTOPFOLDER = 16 CSIDL_DRIVES = 17 CSIDL_NETWORK = 18 CSIDL_NETHOOD = 19 CSIDL_FONTS = 20 CSIDL_SHELLNEW = 21 End Enum Private Const FO_MOVE = &H1 Private Const FO_RENAME = &H4 Private Const FOF_SILENT = &H4 Private Const FOF_NOCONFIRMATION = &H10 Private Const FOF_RENAMEONCOLLISION = &H8 Private Const MAX_PATH As Integer = 260 Private Const SHARD_PATH = &H2& Private Const SHCNF_IDLIST = &H0 Private Const SHCNE_ALLEVENTS = &H7FFFFFFF Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAborted As Boolean hNameMaps As Long sProgress As String End Type Private Type SHITEMID cb As Long abID As Byte End Type Private Type ITEMIDLIST mkid As SHITEMID End Type Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _ (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long Private Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _ "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _ ByRef ppidl As Long) As Long Private Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _ ByVal dwdata As String) As Long Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _ (lpFileOp As SHFILEOPSTRUCT) As Long Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _ ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _ (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Function fGetSpecialFolder(CSIDL As Long) As String Dim sPath As String Dim IDL As ITEMIDLIST fGetSpecialFolder = "" If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then sPath = Space$(MAX_PATH) If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\" End If End If End Function Public Function CreateShortcut(ByRef txtFilePath As String, _ ByRef txtName As String, ByRef vTarget As CSIDL_FOLDERS) Dim I As Integer Dim lResult As Long Dim lpil As Long Dim sFilePath As String Dim sFileName As String Dim sRecentPath As String Dim sDesktopPath As String Dim sFilePathOld As String Dim sFilePathNew As String Dim sShortCutName As String Dim SMsg As String Dim SHFileOp As SHFILEOPSTRUCT On Error GoTo cmdCreateError Screen.MousePointer = vbHourglass sFilePath = Trim$(txtFilePath) sShortCutName = Trim$(txtName) & ".lnk" sRecentPath = fGetSpecialFolder(CSIDL_RECENT) sDesktopPath = fGetSpecialFolder(VTarget) SMsg = "Error retrieving folder location." If sRecentPath <> "" And sDesktopPath <> "" Then sMsg = "Error adding shortcut to the Recent File list." lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath) Call Sleep(1500) If lResult Then
I = 1 sFileName = sFilePath Do While I I = InStr(1, sFileName, "\") If I Then sFileName = Mid$(sFileName, I + 1) Loop
sFilePath = sRecentPath & "\" & sFileName & ".lnk" & _ vbNullChar & vbNullChar
With SHFileOp .wFunc = FO_MOVE .pFrom = sFilePath .pTo = sDesktopPath .fFlags = FOF_SILENT End With
SMsg = "Error creating desktop shortcut." lResult = SHFileOperation(SHFileOp) Sleep (1500) If lResult = 0 Then
sFilePathOld = sDesktopPath & "\" & sFileName & ".lnk" & _ vbNullChar & vbNullChar sFilePathNew = sDesktopPath & "\" & sShortCutName & _ vbNullChar & vbNullChar With SHFileOp .wFunc = FO_RENAME .pFrom = sFilePathOld .pTo = sFilePathNew .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION End With SMsg = "Error renaming desktop shortcut." lResult = SHFileOperation(SHFileOp) SMsg = "" Call SHGetSpecialFolderLocationD(0, CSIDL_DESKTOP, lpil)
Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
End If End If End If Screen.MousePointer = vbDefault Exit Function
cmdCreateError: MsgBox "Error creating desktop shortcut. " & SMsg, vbExclamation, "Create Shortcut" End Function
James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.
Comments
-
Posted by hollering on 18 Mar 2005
I need to be able to change the "Start In" field of a shortcut with VBA so that I can automatically create shortcuts for users using AccessRT. Can anyone suggest a way to do this? By the way, great ... -
Posted by fei on 13 Feb 2003
how to get target info (name, location ...) of a shorcut file? -
Posted by Bharatvohra on 15 May 2002
I used the code for "Create a shortcut" given on http://www.vbweb.co.uk/show/245/ . I made it DLL so that i call the dll in my ASP page. But when i'm calling the DLL in ASP page , i'm getting the fo...
|
Search
Related Content
Code Samples
New Members
|