Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default 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.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 147
Default 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.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default 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.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default 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.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 147
Default 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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desktop path? Charlotte E.[_2_] Excel Programming 4 September 2nd 08 05:16 AM
Remove end folder from path found with ThisWorkbook.Path command ? dim Excel Programming 9 April 23rd 08 06:04 AM
EXCEL FILES SAVED TO DESKTOP WILL NOT OPEN FROM DESKTOP randy111 Excel Discussion (Misc queries) 3 January 13th 08 10:38 PM
How to path to user's desktop 1scant[_8_] Excel Programming 2 June 16th 06 03:52 PM
hyperlink navigation path path wrong in Excel 2003 CE Admin Excel Discussion (Misc queries) 5 January 7th 06 07:47 PM


All times are GMT +1. The time now is 08:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"