Click to See Complete Forum and Search --> : Creating Shortcuts
Erik Droog
January 14th, 2000, 05:41 AM
I've found lotsa code about creating shortcuts but I'm missing one thing in these codes; how to set the 'Start In' folder property.
Does anybody have an idea ???
Thanx,
Erik
Chris Eastwood
January 14th, 2000, 05:55 AM
(Taken from my article at : http://codeguru.developer.com/vb/articles/1923.shtml )
If you are using VB6 (or have installed the Windows Scripting Host), creating desktop shortcuts is easy :
'
' Make sure that you are referencing the
' windows scripting host : WSHOM.OCX
'
Dim oShell as IWshShell_Class ' our shell object
Dim oShortcut as IWshShortcut_Class ' our ShortCut object
Dim sDesktop as string ' path of desktop
Dim sTargetPath as string ' path to notepad.exe
'
on error GoTo vbErrorHandler
'
set oShell = new IWshShell_Class ' Create shell object
'
' get desktop path
'
sDesktop = oShell.SpecialFolders.Item("Desktop")
'
' Create shortcut on desktop
'
set oShortcut = oShell.CreateShortcut(sDesktop & "\npad.lnk")
'
' Now fill in the shortcut properties
'
With oShortcut
sTargetPath = oShell.ExpandEnvironmentStrings("%windir%")
.TargetPath = sTargetPath & "\notepad.exe" ' change this to whatever you like
.Arguments = "" ' ditto again !
.WorkingDirectory = sDesktop ' as above!
.Hotkey = "Ctrl + Alt + A"
.Save
End With
Exit Sub
'
vbErrorHandler:
MsgBox Err.Description, , "Create Shortcut"
The '.WorkingDirectory' is the same as 'Start Location' - the scripting host has tonnes of other useful features too.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
January 14th, 2000, 09:28 AM
' i just cuted an pasted from my shortcut creator thing
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
'
' Add a shortcut to the Windows desktop.
'
If Trim$(txtApp) = "" Then Exit Sub
If Trim$(txtName) = "" Then Exit Sub
On Error GoTo cmdCreateError
Screen.MousePointer = vbHourglass
sFilePath = Trim$(txtApp)
sShortCutName = Trim$(txtName) & ".lnk"
'
' Get the paths of the folders to add the shortcuts to.
' The folders are the Recent Files List and the Desktop.
'
sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
sDesktopPath = fGetSpecialFolder(CSIDL_DESKTOPDIRECTORY)
sMsg = "Error retrieving folder location."
If sRecentPath <> "" And sDesktopPath <> "" Then
'
' Create a shortcut in the Recent Files list. The
' SHARD_PATH flag says that the following parameter
' is a path.
'
sMsg = "Error adding shortcut to the Recent File list."
lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
Sleep (1500)
If lResult Then
'
' Extract the .exe name from the path.
'
i = 1
sFileName = sFilePath
Do While i
i = InStr(1, sFileName, "\")
If i Then sFileName = Mid$(sFileName, i + 1)
Loop
'
' Move the shortcut from the Recent folder to the Desktop.
' Since the shortcut now resides in the Recent folder,
' modify the file name to include the Recent folder
' path. Also, append ".lnk" to the original filename.
'
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
'
' Rename the link.
'
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) '123 = can't rename.
sMsg = ""
'
' Refresh the desktop to display the shortcut.
'
Call SHGetSpecialFolderLocationD(Me.hwnd, CSIDL_DESKTOP, lpil)
Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
End If
End If
End If
Screen.MousePointer = vbDefault
Exit Sub
cmdCreateError:
MsgBox "Error creating desktop shortcut. " & sMsg, vbExclamation, "Create Shortcut"
Erik Droog
January 17th, 2000, 04:29 AM
It seems to me that it isn't possible to set the 'Start In' folder with this code. So I prefer the code posted by Chris but thanx anyway
Erik
Erik Droog
January 17th, 2000, 04:30 AM
Works great !! Thanx
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.