Environ Command In Vba To Get Logged Windows User Name

  • Got to say, instead of messing around with API for finding logged in windows user name, simply use this short code. So simple my grandma could use it.


    Code
    Function UNameWindows() As String
        UNameWindows = Environ("USERNAME")
    End Function


    Couldn't get much simpler. Also many other things which can be extracted from the ENVIRON command. Here they are.


    Code to View All Available ENVIRON info


    Code
    Sub ENVIRON_DUMP()
    Dim nCount As Integer
    nCount = 1
        Do Until Environ(nCount) = ""
            Cells(nCount, 1).Value = Environ(nCount)
               nCount = nCount + 1
        Loop
    End Sub



    Enjoy.....................

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Environ Command In Vba To Get Logged Windows User Name


    Thanks. Haven't got a need to use it but sure will come in handy when the time comes!

  • Re: Environ Command In Vba To Get Logged Windows User Name


    Simple, yes. But beware when using the Environ function in general: the environment variables may not be present in all systems, and some are editable by users, so check your output and have a API fallback.


    Using Environ("USERNAME") should be safe, though - although it may return "Administrator" on some Windows Server configurations, even though the user logs on with a different name.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!