Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 23

Thread: Real user name

  1. #1
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location

    Real user name

    hello,
    is it possible to determine the windows(XP) log in name ?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    MsgBox Environ("UserName")
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    MsgBox Environ("USERNAME")
    I get the user name.
    However I changed the user name in control panel/users to another name.
    How can I get the name that is now displayed and not thhe origional user name ?
    Is this possible ?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I am not about to change my username like that to test this, but you could try it

    [vba]
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
    (ByVal lpBuffer As String, nSize As Long) As Long
    Function UserNameWindows()
    Dim lngLen As Long
    Dim strBuffer As String
    Const dhcMaxUserName = 255
    strBuffer = Space(dhcMaxUserName)
    lngLen = dhcMaxUserName
    If CBool(GetUserName(strBuffer, lngLen)) Then
    UserNameWindows = Left$(strBuffer, lngLen - 1)
    Else
    UserNameWindows = ""
    End If
    End Function
    [/vba]

  5. #5
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    Same result

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    How about this article from the KB?

    Function to return various Environment Names
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by johnny44
    Same result
    That suggests to me that the OS doesn't know about it even, and won't until next login, by which time the Environ option will work.

  8. #8
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    This seems to do it...
    Application.UserName

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by johnny44
    This seems to do it...
    Application.UserName
    That is not the Windows (XP) login name as you originally said, it is internal to Excel and can be changed by the user at any time.

  10. #10
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Exactly.. the entry:
    http://vbaexpress.com/kb/getarticle.php?kb_id=768

    That shows the different methods available, and what it is pulling. Since they pull from the windows login, they won't change until the user logs off and logs back on. The Application.Username is the office name, and as Bob says the user can change that easily by many ways.

    Matt

  11. #11
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    Well noted...
    So how could I get the user XP log in name that shows on the XP login Screen ?

    the reg key seems to be this one :
    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Johnny

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by johnny44
    MsgBox Environ("USERNAME")
    I get the user name.
    However I changed the user name in control panel/users to another name.
    How can I get the name that is now displayed and not thhe origional user name ?
    Is this possible ?
    What happens when you log off and log in again? I'd bet it's updated to the new name then? If it is, how often do you expect this to happen?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  13. #13
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    I dont want to create new XP users, So I changed the names on the various xp stations to reflect the current name of the user.

    So if a profile was created as "john" and the the name is changed via control panel/user accounts/change account/change my name to "wilma"

    I would need to capture the wilma value from my xp stations.

    So far I can only get the john even though it has been changed to wilma.
    I think I read that the only way is to re create the account. But I figured if XP shows wilma there must be a way to get that same value...

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Well, here is another attempt (not holding my breath if the API version doesn't work).

    [vba]
    Private Declare Function GetEnvironmentVariable Lib "kernel32" _
    Alias "GetEnvironmentVariableA" _
    (ByVal lpName As String, _
    ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

    Sub xx()
    MsgBox GetEnvironmentVar("UserName")
    End Sub

    Function GetEnvironmentVar(Name As String) As String
    GetEnvironmentVar = String(255, 0)
    GetEnvironmentVariable Name, GetEnvironmentVar, Len(GetEnvironmentVar)
    GetEnvironmentVar = TrimNull(GetEnvironmentVar)
    End Function

    Private Function TrimNull(item As String)
    Dim iPos As Long
    iPos = InStr(item, vbNullChar)
    TrimNull = IIf(iPos > 0, Left$(item, iPos - 1), item)
    End Function
    [/vba]

  15. #15
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    Sory...did not work...still gave ne the "origional" name of account

  16. #16
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    I think the only way is to get from registrie ?

  17. #17
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by johnny44
    Sory...did not work...still gave ne the "origional" name of account
    Can you just confirm? You definately logged out and back in under the new name, correct?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  18. #18
    VBAX Regular
    Joined
    Apr 2006
    Posts
    10
    Location
    Yes I re booted and I the sub still returns the "Origional" name

  19. #19
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by johnny44
    Yes I re booted and I the sub still returns the "Origional" name
    I find that hard to believe. That is akin to saying that logon as another user will give you the original user, which is impossible (I think ).

  20. #20
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Bloody hell. I just tried it (just login, not re-boot), and he is right, it still returns my original name. What is going on?

Posting Permissions

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