Help! I'm trying to get the user login name from an Excel VBA procedure. We were recently updated to 2000 and mstrMyName = Application.UserName only produces "Administrator". Any suggestions!
Yes, Application.Username is going to return the name of the person who installed or owns the software. You will need to set a reference to the Active DS Type Library and look for the object: IADsUser in the Object Browser. Tao is the mobius. The ribbon of heaven and earth, you cannot lose your way.
Following code will work in any OS that has the Username in the environment variables (type SET at command prompt). I think NT/2k/Xp have it:
user=environ("username"
Alternately, the following should work in NT/9x/me/xp/2k:
Private Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Sub test()
user = UserName
MsgBox (user & " is currently logged into this machine."
End Sub
Public Property Get UserName() As String
Const ULEN As Long = 257
Dim s As String * ULEN, l As Long
GetUserName s, ULEN
l = InStr(1, s, Chr(0), vbBinaryCompare)
If l > 1 Then
UserName = Left$(s, l - 1)
End If
End Property
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.