Path to desktop

R

Robert Crandal

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.

Thanks in advance.
 
C

Claus Busch

Hi Robert,

Am Thu, 14 Mar 2013 10:14:50 -0700 schrieb Robert Crandal:
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.

try:
myPath = "C:\Users\" & Environ("UserName") & "\Desktop\"
or select in Windows explorer the desktop, press Shift + right click =>
"Copy as path" and paste it in your module


Regards
Claus Busch
 
W

witek

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.

Thanks in advance.

in 99%, you will find it here:

environ("USERPROFILE")+"\desktop"


the other 1% is when desktop is redirected to other location.
There is no environment variable pointing to desktop directly
probably it can be found somewhere in windows settings but I do not know
how.

Also be careful when concatenating folders.
on my computer, environ("USERPROFILE") does not have slash sign at the
end, but some other variables does.
it is better to use Microsoft Scripting Runtime library and
FileSystemObject class from it to concatenate folders and check if
destination exists.
 
A

Auric__

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.)
 
R

Robert Crandal

Auric__ said:
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:

........
(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.)

First, thank you everyone who replied. All info was helpful.....

And, BTW Auric, each user is assigned a directory on the server.
I believe that each user has a Desktop either on the C: drive or
a server directory named \\fserver1 or something similar. I don't
remember exactly, but I will have to check.

I will give this SHGetSpecialFolderPath() API next time I go to work.
Seems odd that USERPROFILE and FileSystemObject and
SHGetSpecialFolderPath() would return different results, but I shall
put this to the test.

Thanks again.
 
W

witek

Robert said:
First, thank you everyone who replied. All info was helpful.....

And, BTW Auric, each user is assigned a directory on the server.
I believe that each user has a Desktop either on the C: drive or
a server directory named \\fserver1 or something similar. I don't
remember exactly, but I will have to check.

I will give this SHGetSpecialFolderPath() API next time I go to work.
Seems odd that USERPROFILE and FileSystemObject and
SHGetSpecialFolderPath() would return different results, but I shall
put this to the test.

Thanks again.

I tested SHGetSpecialFolderPath and it worked great pointing to right
folder. I have desktops redirected to another disk separately from
userprofile and it returned correct folder.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top