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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

windows user name, please! 1

Status
Not open for further replies.

kingz2000

Programmer
May 28, 2002
304
DE
Hi,

using VBA code, how do I obtain the login name of the user. As in, the user name.
Thanks in advance.
 
You may try Environ("USERNAME")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Try? Try? Hmmmm, how odd. I am not sure what trying has to do with it. Environ("UserName") returns the login name...whether you try, or not.

Just kidding.

Gerry
My paintings and sculpture
 
Environ("UserName") returns the login name
Not in a Win9x environment ...
 
this may sound a bit naff but could you ask the user who pressed the button to start the macro? eg
fred = inputbox("blahblah")
providing they know who they are (hot desking and all that)
 
Environ("UserName") returns the login name
Not in a Win9x environment ...

And not on any of the NT-based systems if the user has chosen to change the environment string, which they are perfectly at liberty to do.

Here's one (of several) alternatives:

CreateObject("Wscript.Network").UserName
 
Just to throw it out there...

I have an article at my site which gives a function to return the MS Office username, Windows login name or Computer name. It can be found at
I believe that Chip Pearson also has an example of how to get the Windows username using an API, as an alternate to the Environ("Username") method.

Ken Puls, CMA
 
Actually, Chip doesn't have it. An example of the API route here though:

Code:
Option Explicit

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

Public Function CurrentUser() As String
'*********************************************************
'* Function to get the current logged on user in windows *
'*********************************************************

Dim strBuff As String * 255
Dim X As Long

CurrentUser = ""
X = GetUserName(strBuff, Len(strBuff) - 1)
If X > 0 Then
'Look for Null Character, usually included
X = InStr(strBuff, vbNullChar)
'Trim off buffered spaces too
If X > 0 Then
CurrentUser = UCase(Left$(strBuff, X - 1)) 'UCase is optional ;)
Else
CurrentUser = UCase(Left$(strBuff, X))
End If
End If

End Function

Sourced from VBForums:
Ken Puls, CMA
 
Well, if we want to use the API, here's the short version:

Public Function CurrentUser() As String

Dim UserName As String * 1024
Dim lenUserName As Long

lenUserName = Len(UserName)
If GetUserName(UserName, lenUserName) Then CurrentUser = Left(UserName, lenUserName - 1)

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top