Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting windows login name (2000)

Status
Not open for further replies.

idono

Technical User
Jan 16, 2002
71
0
0
US
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!
 
if you have a way of getting/reading environment variables then %username% will result in the currently logged in user.
Doug
dxd_2000@yahoo.com

 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top