21 | 09 | 2017

Get Username

Use the routine below to get the active Username


Private Const UNKNOWN = _
"(Value Unknown Because System Call Failed)"

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetCurrentUserName() As String

Dim l As Long
Dim sUser As String

sUser = Space$(255)
l = GetUserName(sUser, 255)

'strip null terminator

If l <> 0 Then
GetCurrentUserName = Left(sUser, InStr(sUser, Chr(0)) - 1)
Err.Raise Err.LastDllError, , _
"A system call returned an error code of " _
& Err.LastDllError
End If

End Function

Sign up now and upload your code to the website.

Help us to continue.....
Articles View Hits
Latest Articles