Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

short cut for path to My Documents in XP

I've been told that there's some kind of abreviated way to indicate the XP path to My Documents. Something possibly with the "%". I'd appreciate details if such a shortcut exists.
Thanks
[212 byte] By [jac] at [2007-11-11 7:37:24]
# 1 Re: short cut for path to My Documents in XP
Try the following:

Environ("USERPROFILE") & "\My Documents"
pclement at 2007-11-11 17:26:37 >
# 2 Re: short cut for path to My Documents in XP
Due to the dynamic nature of Windows with its ability to allow users to put their My Documents folder almost anywhere, Paul's suggestion to use the Environ function will only work if the user has not changed anything. The code below will return the folder no matter where it is!

Option Explicit

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long

Private Declare Function SHGetFolderPath Lib "shfolder.dll" _
Alias "SHGetFolderPathA" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
ByVal hToken As Long, _
ByVal dwReserved As Long, _
ByVal lpszPath As String) As Long

Private Sub Command1_Click()
Dim buff As String
'Allocate buffer space
buff = Space$(260)
If SHGetFolderPath(Me.hwnd, &H5, -1, &H0, buff) = 0 Then
Caption = TrimNull(buff)
Else
Caption = "Sorry - Not Found"
End If
End Sub

Private Function TrimNull(startstr As String) As String
TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function

This code is a cut down version of the excellent code on Randy Birch's VBNet.
www.mvps.org/

Steve. :cool:
Steve Grant at 2007-11-11 17:27:48 >
# 3 Re: short cut for path to My Documents in XP
My thanks to you both. I can see how both methods will be good to know for future reference.

Thanks!
jac at 2007-11-11 17:28:48 >
# 4 Re: short cut for path to My Documents in XP
Just an FYI with respect to SHGetFolderPath, it was added in Windows 2000 so if your app is to run on Windows 95, 98 or ME you have to distribute the SHFolder.dll file.

Also, there was a security update that caused a glitch with this API. A hotfix or IE SP may be required.

http://support.microsoft.com/default.aspx?scid=kb;en-us;303437

Remember that just as the system environment variables rely on values stored in the Registry, so does SHGetFolderPath. If the user reconfigures his underlying My Documents folders improperly all bets are off.

For this reason, if you really want to be thorough, use the Dir command to verify that the folder path returned is valid, regardless of what method you use. If it isn't valid then pop up a dialog requesting that the path be specified.
pclement at 2007-11-11 17:29:43 >
# 5 Re: short cut for path to My Documents in XP
Thanks for the update Paul.

I was aware that the SHGetFolderPath was only available from 2000 onwards and should have pointed that out to Jac. But the dll is readily distributable and I have always thought it would return the folder, no matter what the user has done.

Can you ellucidate further?

Many Thanks,

Steve. :cool:
Steve Grant at 2007-11-11 17:30:41 >