How do I get the current user name in VBA?

橙三吉。 提交于 2019-12-24 04:34:10

问题


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.


回答1:


I believe it's something like

Environ("Username")




回答2:


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.

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))


来源:https://stackoverflow.com/questions/6255535/how-do-i-get-the-current-user-name-in-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!