if_zero_equals_one
if_zero_equals_one

Reputation: 1774

How do I get the current user name in VBA?

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

Answers (3)

ainwood
ainwood

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

GSerg
GSerg

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

lucks
lucks

Reputation: 976

I believe it's something like

Environ("Username")

Upvotes: 19

Related Questions