Reputation: 1774
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.
Upvotes: 5
Views: 36145
Reputation: 1048
Similar to accepted answer (Environ("username"), there is an environment variable available for the App Data folder, so you can get it using:
Environ("APPDATA")
FYI, here is a good link for showing the full list of Environ Variables. There are a few that are windows universal, and some that can be registered as being application specific.
Upvotes: 1
Reputation: 78155
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))
Upvotes: 10