Page 1 of 2 12 LastLast
Results 1 to 10 of 15
Like Tree1Likes

Get Windows user name

This is a discussion on Get Windows user name within the Excel Questions forums, part of the Question Forums category; Is there a way I can get the name of the user logged onto Windows to use in Excel VBA ...

  1. #1
    New Member
    Join Date
    Mar 2002
    Posts
    34

    Default

    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

  2. #2
    Guest

    Default

    Application.UserName

  3. #3
    Board Regular
    Join Date
    Feb 2002
    Location
    Guderup, Denmark
    Posts
    287

    Default

    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

  4. #4
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701

    Default

    The following code returns the username of excel not windows:
    Application.UserName

    I hope this helps.
    Kind regards,
    Al

  5. #5
    Guest

    Default

    Application.UserName

  6. #6
    Board Regular
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    327

    Default

    On Excel running over a network, I had occasion to use this where UserName returned useless info like the company name.

    Sh.[A1] = UserLoggedIn

  7. #7
    Board Regular
    Join Date
    Jul 2002
    Location
    Charlotte, NC
    Posts
    417

    Default

    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

  8. #8
    MrExcel MVP lenze's Avatar
    Join Date
    Feb 2002
    Location
    Helena, MT
    Posts
    13,690

    Default

    The code to return the log-on name is
    Code:
    Environ("UserName")
    Code:
    Application.UserName
    returns the application name set under Tools>Options

    lenze
    If you have to tell your boss you're good with Excel, you're NOT!!
    All I know about Excel I owe to my ignorance!
    Scotch: Because you don't solve great Excel problems over white wine

  9. #9
    Board Regular
    Join Date
    Jul 2002
    Location
    Charlotte, NC
    Posts
    417

    Default

    I like simple. Thank you.

  10. #10
    Board Regular Grizlore's Avatar
    Join Date
    Aug 2006
    Location
    Rose of the Shires
    Posts
    241

    Default

    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
    Pondus72 likes this.
    GriZlore


    Statistics are like a lamp-post to a drunken man - more for leaning on than illumination.

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com