ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Path to desktop (https://www.excelbanter.com/excel-programming/448407-path-desktop.html)

Robert Crandal[_2_]

Path to desktop
 
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.



Claus Busch

Path to desktop
 
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
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2

witek

Path to desktop
 
Robert Crandal wrote:
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 he

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.



Auric__

Path to desktop
 
Robert Crandal wrote:

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

--
He's the friend who'll listen, although you may wish he hadn't.

Robert Crandal[_2_]

Path to desktop
 
"Auric__" wrote :

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.



witek

Path to desktop
 
Robert Crandal wrote:
"Auric__" wrote :

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.



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.




All times are GMT +1. The time now is 11:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com