所以我们有一个系统要求用户登录并且它有自己的漫游配置文件。那么在这段代码中,我如何定位当前用户文档文件夹? (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)
该函数的其余部分做得很好...它打开文件资源管理器我只是无法找到告诉它寻找当前用户的语法。
可能最好的方法是使用这样的函数:
Function docsFolder() As String
docsFolder = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function
还有其他方法,但这个方法适用于任何版本的Windows和用户自定义。
例如,在我的情况下,我在映射的X:
驱动器上有我的文档文件夹,所以简单地将我的用户名填入C:\
路径是行不通的。
SpecialFolders
Property我不确定你希望它有多灵活,但你可以尝试以下方法
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
得到它了!谢谢!对于任何可能关心的人......让她跑的最后一串!
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