如何定位当前用户的文档文件夹?

问题描述 投票:1回答:3

所以我们有一个系统要求用户登录并且它有自己的漫游配置文件。那么在这段代码中,我如何定位当前用户文档文件夹? (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)

该函数的其余部分做得很好...它打开文件资源管理器我只是无法找到告诉它寻找当前用户的语法。

excel vba excel-vba exacttarget file-location
3个回答
1
投票

可能最好的方法是使用这样的函数:

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

还有其他方法,但这个方法适用于任何版本的Windows和用户自定义。

例如,在我的情况下,我在映射的X:驱动器上有我的文档文件夹,所以简单地将我的用户名填入C:\路径是行不通的。


更多信息:


0
投票

我不确定你希望它有多灵活,但你可以尝试以下方法

strRootPath = "C:\Users\" + Environ("Username") + "\Documents"

0
投票

得到它了!谢谢!对于任何可能关心的人......让她跑的最后一串!

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
© www.soinside.com 2019 - 2024. All rights reserved.