Get Windows user name

matthewh

New Member
Joined
Mar 13, 2002
Messages
34
Is there a way I can get the name of the user logged onto Windows to use in Excel VBA to name files, put the username into macros and formulas, etc. I have many uses for this kind of functionality in Excel and (if not too off topic) Access as well. I'm sure I've seen this elsewhere before, but can't remember where or how. We use Office 2000 on Win98 and 2000 workstations.

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this function GetUserName()

Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function
 
Upvote 0
The following code returns the username of excel not windows:
Application.UserName

I hope this helps.
Kind regards,
Al
 
Upvote 0
On Excel running over a network, I had occasion to use this where UserName returned useless info like the company name.

Sh.[A1] = UserLoggedIn
 
Upvote 0
Try this function GetUserName()

Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function

Where do you put this?

If we have a button on a worksheet that presents a msgbox with the username where do I put the above code?

Thank you,
Michael
 
Upvote 0
The code to return the log-on name is
Code:
Environ("UserName")

Code:
Application.UserName
returns the application name set under Tools>Options

lenze
 
Upvote 0
I found this solution worked well, as Environ("Username") just returned the Users login initials, and not the Users actual name.

Code:
Option Explicit 
  
Sub Users_Fullname() 
'originally coded as VB script by A.Vials, converted to VBA by Sly 
Dim objInfo 
Dim strLDAP 
Dim strFullName 
  
Set objInfo = CreateObject("ADSystemInfo") 
strLDAP = objInfo.UserName 
Set objInfo = Nothing 
strFullName = GetUserName(strLDAP) 
  
MsgBox "Full name of User is " & strFullName  'step to test 
  
End Sub 

Function GetUserName(strLDAP) 
  Dim objUser 
  Dim strName 
  Dim arrLDAP 
  Dim intIdx 
  
  On Error Resume Next 
  strName = "" 
  Set objUser = GetObject("LDAP://" & strLDAP) 
  If Err.Number = 0 Then 
    strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn") 
  End If 
  If Err.Number <> 0 Then 
    arrLDAP = Split(strLDAP, ",") 
    For intIdx = 0 To UBound(arrLDAP) 
      If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then 
        strName = Trim(Mid(arrLDAP(intIdx), 4)) 
      End If 
    Next 
  End If 
  Set objUser = Nothing 
  
  GetUserName = strName 
  
End Function
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top