Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Aug 2002
    Location
    Harrisburg, Pennsylvania, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get Windows user name? (VBA/Excel 2000 sr-1)

    Is it possible to get the current Windows user name? I.e., if I log in to my machine as "Cristina", then how to get back "Cristina"?

    Please be aware, question coming from a self-taught VBA programmer with limited knowledge of the wider Windows world ... so if the answer (as I suspect) leads off into arcane topics like API functions or the Windows registry, please type slowly and use small words?

    Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    Copy/paste the following declaration to the top of a module, that is below Option Explicit (if you have that), but above all Subs and Functions:

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

    Copy/paste the following function somewhere below it:

    Public Function WindowsUser() As String
    Dim strUsername As String
    strUsername = String(100, vbNullChar)
    GetUserName strUsername, 100
    WindowsUser = Left$(strUsername, InStr(strUsername, vbNullChar) - 1)
    End Function

    You can use this function to retrieve the Windows username, for example:

    Sub Test()
    MsgBox "Hello there, " & WindowsUser
    End Sub

  3. #3
    Lounger
    Join Date
    Aug 2002
    Location
    Harrisburg, Pennsylvania, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    Beautiful. That's exactly what I need -- thank you very much!

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    A slightly simpler way is to use the VBA Environ function:
    <pre>MsgBox Environ("USERNAME")
    </pre>


  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    Provided that it is set, which it usually is in a Windows Server environment. Another obvious method with a Microsoft Application (the OP specified Excel) is:

    strUsername = Application.UserName

    <img src=/S/doh.gif border=0 alt=doh width=15 height=15> <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>
    -John ... I float in liquid gardens
    UTC -7ąDS

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    Application.Username doesn't give the Windows Username of the logged in user, it gives the value of Tools > Options > User Information

    StuartR

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Get Windows user name? (VBA/Excel 2000 sr-1)

    I'm out of my depth in the Server environment, but in my company I can't log on to SuzyQ's computer as JohnBF without having a new JohnBF profile created (or using an existing one if I have previously logged on), and in that case the Application.Username reflects the current user profile. (It had better, or the opportunities for sabotage are limitless. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>)

    Outside that Server environment you are correct, when I use the Bride's home PC, all my work product shows the Application.Username as "BrideofJohnBF". <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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