how can a target the current user's documents folder?

1.7k Views Asked by At

So we have a system that requires users to log in and it has there own roaming profile. So in this string of code how can i target the current users document folder? (FYI excel 2010)

'WORKAROUND:
Dim PID As Double
Dim strRootPath As String

Const strExpExe = "explorer.exe"
Const strArg = " "    '" /e,/root, "

'this is where i need to figure out how to target current user's documents
'// Change rootpath here
strRootPath = "C:\Data Files"

PID = Shell(strExpExe & strArg & strRootPath, 3)

the rest of the function does great... it opens file explorer i just cant figure the syntax for telling it to look for the current user.

3

There are 3 best solutions below

4
ashleedawg On BEST ANSWER

Probably the best way would be with a function like this:

Function docsFolder() As String
    docsFolder = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function

There are other ways too but this one will work on any version of Windows and with user customizations.

For example, in my case, I have my documents folder on a mapped X: drive, so simply stuffing my username into a C:\ path would not work.


More Information:

0
ifo20 On

I'm not sure how flexible you want this to be but you could try the following

strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
0
Daniel Roy On

Got it! thanks! for anyone that might care... The final string that made her run!

Function docsFolder() As String
docsFolder = 

CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function




Private Sub test()

Dim PID As Double
Dim strRootPath As String

Const strExpExe = "explorer.exe"
Const strArg = " "    '" /e,/root, "


'// Change rootpath here
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"

PID = Shell(strExpExe & strArg & strRootPath, 3)

End Sub