如何在 VBA 中获取当前用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6255535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the current user name in VBA?
提问by if_zero_equals_one
I just need to get the name of the current user so I can access the app data folder within their folders.... I have to do this in VBA so yeah...help please.
我只需要获取当前用户的名称,以便我可以访问他们文件夹中的应用程序数据文件夹......我必须在 VBA 中执行此操作,所以是的......请帮忙。
回答by lucks
I believe it's something like
我相信这就像
Environ("Username")
Environ("Username")
回答by GSerg
You do not need user name to know which folder is app data folder.
您不需要用户名即可知道哪个文件夹是应用程序数据文件夹。
You need to use the SHGetFolderPath
function with the CSIDL_APPDATA
value.
您需要使用SHGetFolderPath
带有CSIDL_APPDATA
值的函数。
Private Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwnd As Long, ByVal csidl As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
Private Const CSIDL_APPDATA As Long = &H1A
Private Const MAX_PATH As Long = 260
Dim s As String
s = String$(MAX_PATH, 0)
SHGetFolderPath 0, CSIDL_APPDATA, 0, 0, s
MsgBox Left$(s, InStr(1, s, vbNullChar))