Username in path

P

PSM

I want to create a button to save the file to the user desktop. THi
means the username is the variable part of the save string.
How do I insert this into the path string
 
R

Rick Rothstein

Copy/Paste the code below into a Module (click Insert/Module from the VB
editor's menu bar). Then calling the GetDesktopPath function will return the
path to the user's desktop (no matter which operating system they are
using). Note... the returned path will not have a trailing backspace at the
end, so if you are concatenating this path with a filename, don't forget to
add the backspace into the concatenation somewhere.

'*************** START OF CODE ***************
Private Declare Function SHGetFolderPath Lib "shfolder" _
Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, _
ByVal nFolder As Long, ByVal hToken As Long, _
ByVal dwFlags As Long, ByVal pszPath As String) As Long

Private Const CSIDL_DESKTOPDIRECTORY As Long = &H10

Function GetDesktopPath() As String
Dim strPath As String
Dim lngReturn As Long
strPath = String(255, 0)
lngReturn = SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, 0, strPath)
GetDesktopPath = Left$(strPath, InStr(1, strPath, Chr(0)) - 1)
End Function
'*************** END OF CODE ***************
 
H

Harlan Grove

Rick Rothstein said:
'*************** START OF CODE ***************
Private Declare Function SHGetFolderPath Lib "shfolder" _
        Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, _
        ByVal nFolder As Long, ByVal hToken As Long, _
        ByVal dwFlags As Long, ByVal pszPath As String) As Long

Private Const CSIDL_DESKTOPDIRECTORY As Long = &H10

Function GetDesktopPath() As String
  Dim strPath As String
  Dim lngReturn  As Long
  strPath = String(255, 0)
  lngReturn = SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, 0, strPath)
  GetDesktopPath = Left$(strPath, InStr(1, strPath, Chr(0)) - 1)
End Function
'*************** END OF CODE ***************
....

This works, but

Function foo() As String
foo = Environ("USERPROFILE") & "\Desktop"
End Function

is arguably easier to understand and maintain. Granted users outside
environments governed by group policies could edit their Windows
registry to change their desktop folder from the default Desktop
subdir within their profile dir, so the simpler approach could return
the wrong result.

That said, I personally REALLY, REALLY HATE any application dropping
anything on my desktop. User's My Documents directory would usually be
a better place IF users' local folders were automatically backed up.
If not, better to save the files into users' file server home
directories which presumably are backed up automatically as part of
their file servers' automatic backup. If necessary, put shortcuts to
these files on users' desktops.
 
R

Rick Rothstein

Interesting... here you are advocating using a method that might not be
guaranteed to work because it is simpler whereas in the "Isolate text in a
long url" thread you are advancing what appears to be an opposite position.

By the way, I completely agree with you on being against programs that drop
files on one's Desktop... my defense is that I was just answering the
question that was asked.<g>

--
Rick (MVP - Excel)


Rick Rothstein said:
'*************** START OF CODE ***************
Private Declare Function SHGetFolderPath Lib "shfolder" _
Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, _
ByVal nFolder As Long, ByVal hToken As Long, _
ByVal dwFlags As Long, ByVal pszPath As String) As Long

Private Const CSIDL_DESKTOPDIRECTORY As Long = &H10

Function GetDesktopPath() As String
Dim strPath As String
Dim lngReturn As Long
strPath = String(255, 0)
lngReturn = SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, 0, strPath)
GetDesktopPath = Left$(strPath, InStr(1, strPath, Chr(0)) - 1)
End Function
'*************** END OF CODE ***************
....

This works, but

Function foo() As String
foo = Environ("USERPROFILE") & "\Desktop"
End Function

is arguably easier to understand and maintain. Granted users outside
environments governed by group policies could edit their Windows
registry to change their desktop folder from the default Desktop
subdir within their profile dir, so the simpler approach could return
the wrong result.

That said, I personally REALLY, REALLY HATE any application dropping
anything on my desktop. User's My Documents directory would usually be
a better place IF users' local folders were automatically backed up.
If not, better to save the files into users' file server home
directories which presumably are backed up automatically as part of
their file servers' automatic backup. If necessary, put shortcuts to
these files on users' desktops.
 
H

Harlan Grove

Rick Rothstein said:
Interesting... here you are advocating using a method that might not be
guaranteed to work because it is simpler whereas in the "Isolate text in a
long url" thread you are advancing what appears to be an opposite position.

Who says I need to be consistent?

In corporate and governmental computing environments group policies
generally inhibit or actually prevent users from changing this sort of
basic configuration. So the odds are that my simplified approach in
this thread would almost always work. OTOH, unless the OP in the other
thread were parsing urls generated by his own company's web or
intranet server, s/he has no control over the possible urls s/he needs
to process. The odd there no longer favor simple approaches.

On a statistical basis, I'll claim my trade-off between simplicity and
robustness beats yours.
By the way, I completely agree with you on being against programs that drop
files on one's Desktop... my defense is that I was just answering the
question that was asked.<g>

The cost of free answers in newsgroups is free advice.
 

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