Robert said:
Using VBA, what is a good way to obtain the path to the
user's current Windows 7 desktop? Once I have the
path, I would like to be copy a few files or shortcuts
onto the user's desktop.
While you usually *can* find it using the already-suggested USERPROFILE
environment variable, or the FileSystemObject, the method I've found to be
"best" (most reliable, whatever) is the SHGetSpecialFolderPath API call:
Declare Function SHGetSpecialFolderPath Lib "SHELL32" _
Alias "SHGetSpecialFolderPathA" ( _
ByVal hwndOwner As Long, ByVal lpszPath As String, _
ByVal nFolder As Long, ByVal fCreate As Long) As Long
Public Const MAX_PATH = 260
Public Const CSIDL_DESKTOPDIRECTORY = 16&
Function whereIsDesktop() As String
desktop$ = Space$(MAX_PATH + 1)
result& = SHGetSpecialFolderPath(0, desktop$, CSIDL_DESKTOPDIRECTORY, 0)
tmp& = InStr(desktop$, Chr$(0))
If tmp& Then desktop$ = Left$(desktop$, tmp& - 1)
whereIsDesktop = desktop$
End Function
Of course, this *probably* won't work if the path contains multi-byte
characters of any kind. (Not a problem for most English-language installs.)
(I use this because I usually change the location of the desktop to a certain
directory on my server. Good luck getting something in C:\users\Auric\Desktop
to show up on my desktop.)