Hello,
I'm using Excel 2003 and want to programmaticly create shortcuts to folders. When I try the program below it works when the destiny is a file, but I need to open Windows Explorer with the shortcut.
When using 'my' code a shortcut is placed in the right folder but when opening it a window pops up and asks with what application I want to open it, instead of Windows Explorer with the target folder.
Looking at the properties of that shortcut it does say it's a folder-type.
So, what is going wrong???
Greetz, CJ.
The shortcuts must be placed in one folder: C:\destiny\~shortcuts\
The folders themselves are in: C:\customers\john doe\
I've marked original with 'ORIGINAL and my work with 'MY EDIT
Sub CreateShortcut()
Dim szMsg As String
Dim szStyle As String
Dim szTitle As String
' Change here for the icon's name
Const szIconName As String = "\cvg.ico"
' Constant string values, you can replace "Desktop"
' with any Special Folders name to create the shortcut there
'ORIGINAL
Const szlocation As String = "Desktop"
' MY EDIT
'Const szlocation As String = "C:\destiny\~shortcuts\"
Const szLinkExt As String = ".lnk"
' Object variables
Dim oWsh As Object
Dim oShortcut As Object
' String variables
Dim szSep As String
Dim szBookName As String
Dim szBookFullName As String
Dim szPath As String
Dim szDesktopPath As String
Dim szShortcut As String
' Initialize variables
'ORIGINAL
szSep = Application.PathSeparator
szBookName = szSep & ThisWorkbook.Name
szBookFullName = ThisWorkbook.FullName
szPath = ThisWorkbook.Path
' MY EDIT
'szSep = Application.PathSeparator
'szBookName = szSep & "customers\johndoe"
'szBookFullName = "\customers\johndoe\"
'szPath = "C:\customers\johndoe\"
On Error Goto ErrHandle
' The WScript.Shell object provides functions to read system
' information and environment variables, work with the registry
' and manage shortcuts
Set oWsh = CreateObject("WScript.Shell")
'ORIGINAL
szDesktopPath = oWsh.SpecialFolders(szlocation)
' MY EDIT
'szDesktopPath = szlocation
' Get the path where the shortcut will be located
szShortcut = szDesktopPath & szBookName & szLinkExt
' Make it happen
Set oShortcut = oWsh.CreateShortCut(szShortcut)
' Link it to this file
With oShortcut
.TargetPath = szBookFullName
.IconLocation = szPath & szIconName
.Save
End With
' Explicitly clear memory
Set oWsh = Nothing
Set oShortcut = Nothing
' Let the user know it was created ok
szMsg = "Shortcut was created successfully"
szStyle = 0
szTitle = "Success!"
MsgBox szMsg, szStyle, szTitle
Exit Sub
' or if it wasn't
ErrHandle:
szMsg = "Shortcut could not be created"
szStyle = 48
szTitle = "Error!"
MsgBox szMsg, szStyle, szTitle
End Sub
Bookmarks