Finding a user's My Documents path

G

Guest

Is there a way to find a user's complete path to the location of their My
Documents folder and assign that path to a variable called pname?

For example, the complete path to my My Documents folder is:
C:\Documents and Settings\bob\My Documents\

So I would want pname = C:\Documents and Settings\bob\My Documents\

Thanks in advance for any help.
Bob
 
G

Gary Keramidas

maybe this

Sub test()
Dim fPath As String
fPath = "C:\Documents and Settings\" & Environ("username") & "\My Documents\"
MsgBox fPath
End Sub
 
G

Guest

Thanks for the link! The solution appears to be far more complex than what I
really need. However, I will retain the link for potential future use.
Bob
 
H

Harlan Grove

Gary Keramidas said:
here' another way
fpath = environ("userprofile") & "\My Documents\"
....

Not guaranteed to work when users change the name of their "My Documents"
folder (which, FWLIW, I've done myself). And I believe it's no longer called
"My Documents" in Vista, but I could be wrong about that.

The only guaranteed correct way to determine a user's "My Documents"
folder's drive/directory path is by going into the Windows Registry, in the

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

key, and finding the value of the Personal value-name.
 
R

Ron de Bruin

"My Documents" in Vista, but I could be wrong about that.

Correct

C:\Users\Ron\Documents


The OP can try this
If I rename my folder to "DocumentsFromRon" the code will work for me Harlan to open the folder

Sub GetSpecialFolder()
'Special folders are : AllUsersDesktop, AllUsersStartMenu
'AllUsersPrograms, AllUsersStartup, Desktop, Favorites
'Fonts, MyDocuments, NetHood, PrintHood, Programs, Recent
'SendTo, StartMenu, Startup, Templates

Dim WshShell As Object
Dim SpecialPath As String

Set WshShell = CreateObject("WScript.Shell")
SpecialPath = WshShell.SpecialFolders("MyDocuments")

'Open folder in Explorer
Shell "explorer.exe " & SpecialPath, vbNormalFocus
End Sub
 
R

Rick Rothstein \(MVP - VB\)

Unless My Documents is moved to other path, this is a special folder,
better
you use API calls to get the correct path, see in www.oaltd.co.uk.

Here is an API method to get the path to "My Documents" (called Documents
now on Vista). I tried posting this same routine twice directly to one of
the OP's messages and, while I got no bounce-back or error, neither message
ever got posted there (at least I can't see them in my newsreader); so, I
figured I would try again here as my code fits your comments...

Copy the code below my signature into your code window and, for an example,
call it this way...

pname = GetMyDocumentsFolder()

from within your own code. Better yet, add a Module to your project
(Insert/Module from VBA's menu) and not only can you call it from within
your own code, but you can call it from a worksheet just like a built-in
function also.

Rick


Private Type SHITEMID
cb As Long
abID As Byte
End Type

Private Type ITEMIDLIST
mkid As SHITEMID
End Type

Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As ITEMIDLIST) As Long

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long

Function GetMyDocumentsFolder() As String
Dim RetVal As Long
Dim Path As String
Dim IDL As ITEMIDLIST
Const CSIDL_PERSONAL = &H5
If SHGetSpecialFolderLocation(0&, CSIDL_PERSONAL, IDL) = 0 Then
Path = Space$(512)
SHGetPathFromIDList IDL.mkid.cb, Path
GetMyDocumentsFolder = Trim$(Replace(Path, Chr$(0), ""))
End If
End Function
 
G

Guest

Ron,
Thanks for pointing out the caveats. I will be sure to use your code below.
Thanks again,
Bob
 

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