Create a desktop shortcut

elgringo56

Well-known Member
Joined
Apr 15, 2002
Messages
869
Can I, using VBA, create a desktop shortcut with icon that will allow the user to double click it open a given folder such as C:\xxxx\yyyy?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I was hopeing for something a little less complicated, as if I had opened explore, right clicked on a file, then selected Send To Desktop as shortcut.
 
Upvote 0
Not sure what you mean by easy, but try the following on for size:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CrtShrt()
<SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, NwShrt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Set</SPAN> a = CreateObject("WScript.Shell")
y = a.SpecialFolders("Desktop")
<SPAN style="color:#00007F">Set</SPAN> NwShrt = a.CreateShortcut(y & "\File.xls.lnk")
<SPAN style="color:#00007F">With</SPAN> NwShrt
    .TargetPath = a.ExpandEnvironmentStrings("c:\temp\test.xls")
    .WorkingDirectory = a.ExpandEnvironmentStrings("c:\temp\test.xls")
    .WindowStyle = 4
    .IconLocation = a.ExpandEnvironmentStrings("%windir%\excel.exe , 0")
    .Save
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Set</SPAN> a = Nothing: <SPAN style="color:#00007F">Set</SPAN> NwShrt = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
Good code Nate, Nice and short...for what it's worth the WshShell cannot Use API's it is limited
Using the WshShell obj gives you only these 16 Special Folders. Sometimes you may require a little
more...........

AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates

A special folder's path depends on the logged on users environment and is unique to him/hers computer system.

Using an API gives you (at least from what I have done / tested) 53 SpecialFolders

<font face=Courier New><SPAN style="color:darkblue">Option</SPAN> <SPAN style="color:darkblue">Explicit</SPAN>

<SPAN style="color:green">'// CSIDL values provide a unique system-independent way to identify special folders</SPAN>
<SPAN style="color:green">'// used frequently by applications, but which may not have the same name or location</SPAN>
<SPAN style="color:green">'// on any given system. For example, the system folder may be</SPAN>
<SPAN style="color:green">'// "C:\Windows\System" on one system and "C:\Winnt\System32" on another, typically Work :)</SPAN>

<SPAN style="color:green">'// Tip:</SPAN>
<SPAN style="color:green">'// Enum the CSIDL constants for easier data changing</SPAN>
<SPAN style="color:green">'// Make sure Auto List Member is Ticked —</SPAN>
<SPAN style="color:green">'// Displays a list that contains information that would logically complete the statement</SPAN>
<SPAN style="color:green">'// at the current insertion point.</SPAN>
<SPAN style="color:green">'// eg Typing in SpecFolders gives you all the Constants below</SPAN>

<SPAN style="color:green">'// Here is The List I was able to gather = 53</SPAN>
<SPAN style="color:green">'// Some of which may NOT be avail for your OS</SPAN>
<SPAN style="color:green">'// Make it Public so it is avail to All your Modules</SPAN>
<SPAN style="color:darkblue">Public</SPAN> <SPAN style="color:darkblue">Enum</SPAN> SpecFolders
    CSIDL_APPDATA = &H1A
    CSIDL_BITBUCKET = &HA
    CSIDL_COMMON_DESKTOPDIRECTORY = &H19
    CSIDL_COMMON_DOCUMENTS = &H2E
    CSIDL_COMMON_FAVORITES = &H1F
    CSIDL_COMMON_PROGRAMS = &H17
    CSIDL_COMMON_STARTMENU = &H16
    CSIDL_COMMON_STARTUP = &H18
    CSIDL_COMMON_TEMPLATES = &H2D
    CSIDL_COMMONALTSTARTUP = &H1E
    CSIDL_COMMONAPPDATA = &H23
    CSIDL_COMMONDESKTOP = &H0
    CSIDL_COMMONMYMUSIC = &H35
    CSIDL_COMMONMYPICTURES = &H36
    CSIDL_COMMONMYVIDEOS = &H37
    CSIDL_COMMONSTARTADMIN = &H2F
    CSIDL_CONNECTIONS = &H31
    CSIDL_CONTROLS = &H3
    CSIDL_DRIVES = &H11
    CSIDL_FAVORITES = &H6
    CSIDL_FONTS = &H14
    CSIDL_LOCALALTSTARTUP = &H1D
    CSIDL_LOCALAPPDATA = &H1C
    CSIDL_LOCALAPPMSCDBURNING = &H3B
    CSIDL_LOCALCOOKIES = &H21
    CSIDL_LOCALDESKTOPDIRECTORY = &H10
    CSIDL_LOCALHISTORY = &H22
    CSIDL_LOCALINTERNETCACHE = &H20
    CSIDL_LOCALMYVIDEOS = &HE
    CSIDL_LOCALSTARTADMIN = &H30
    CSIDL_MSHOME = &H3D
    CSIDL_MYMUSIC = &HD
    CSIDL_MYPICTURES = &H27
    CSIDL_NETHOOD = &H13
    CSIDL_NETWORK = &H12
    CSIDL_PERSONAL = &H5
    CSIDL_PRINTERS = &H4
    CSIDL_PRINTHOOD = &H1B
    CSIDL_PROFILE = &H28
    CSIDL_PROGRAM_FILES = &H26
    CSIDL_PROGRAM_FILES_COMMON = &H2B
    CSIDL_PROGRAM_FILES_COMMONX86 = &H2C
    CSIDL_PROGRAM_FILESX86 = &H2A
    CSIDL_PROGRAMS = &H2
    CSIDL_RECENT = &H8
    CSIDL_RESOURCES = &H39
    CSIDL_SENDTO = &H9
    CSIDL_STARTMENU = &HB
    CSIDL_STARTUP = &H7
    CSIDL_SYSTEM = &H25
    CSIDL_SYSTEMX86 = &H29
    CSIDL_TEMPLATES = &H15
    CSIDL_WINDOWS = &H24
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Enum</SPAN>
</FONT>
 
Upvote 0
NateO, thats great, really works great. Thanks a lot. I would have never in a thousand years figgured that one out. Ivan, what you have there may all be true, but its so far beyond me Im not even sure its written in English. LOL. Thanks guys.
 
Upvote 0
El Gringo, you're welcome.

Ivan, nice to see you and nice tack-on, I shall search for this thread when I'm looking for a folder. :)
 
Upvote 0
Oops, the code worked great in Windows XP, Office 2000 where I initially wrote it, but in Windows 2000, Office 2000 it gives a runtime error. Must be a big difference here somewhere.
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,835
Members
449,343
Latest member
DEWS2031

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top