Results 1 to 12 of 12

Thread: [RESOLVED] Get the Current user

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Resolved [RESOLVED] Get the Current user

    Hi! can anybody please help me..how can i get the current logged in user using macro. any inputs will be greatly appriciated!! thanks in advance!!!

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Get the Current user

    If you mean vba coding - have a look at environ$ function. Usually stored in there somewhere.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Get the Current user

    The current user name is stored in;
    VB Code:
    1. Application.UserName
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get the Current user

    Many methods and code examples posted here - http://vbforums.com/showthread.php?t=357723
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get the Current user

    thanks for that Dkenny but what i want is the current user that is logged into the computer not the username in the application..because i have different username in logging to my computer and the username for my of MS office apps..i will check on that RobDog88..by the way, Ecniv where can i find the environ$ function? thanks anyway to all of you guys!!

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get the Current user

    I cover the environ and well as other methods of obtaining the current windows logged on user in my faq code link.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get the Current user

    ok thanks for that RobDog88...But i have another problem with your code...why is it..hhmmm..let say i store the current user into a variable say 'current_user' and try to append something to it say " the great" (current_user = current_user & " the great") and when i try to display it (msgbox current_user,VbOkOnly) the only thing that it display is the current user but the string that i have appended is missing..can you help me with this? thanks!!!!

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get the Current user

    Sure, just sounds like something is incorrect. Post your code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get the Current user

    here's the code..
    VB Code:
    1. Private Enum EXTENDED_NAME_FORMAT
    2.     NameUnknown = 0
    3.     NameFullyQualifiedDN = 1
    4.     NameSamCompatible = 2
    5.     NameDisplay = 3
    6.     NameUniqueId = 6
    7.     NameCanonical = 7
    8.     NameUserPrincipal = 8
    9.     NameCanonicalEx = 9
    10.     NameServicePrincipal = 10
    11. End Enum
    12.  
    13. Private Declare Function GetUserNameEx Lib "secur32.dll" Alias _
    14. "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, _
    15. ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    16.  
    17. Private Sub Command1_Click()
    18. Dim sBuffer As String, Ret As Long
    19. sBuffer = String(256, 0)
    20. Ret = Len(sBuffer)
    21. If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
    22.      current_user = Left$(sBuffer, Ret) & " the great"
    23. Else
    24.      current_user = Environ("USERNAME") & " the great"
    25. End If
    26.  
    27. msgbox current_user,vbOkOnly
    28. End SUb

    Output: ABS\Administrator
    *Note: "the great" is missing.. I want "'the great" string to appear after 'ABS\Administrator'. so i can have "ABS\Administrator the great".

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get the Current user

    Ok, your using that method so there will be a null character returned and when you append " the great" at the end it gets cut off because of the null.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Enum EXTENDED_NAME_FORMAT
    4.     NameUnknown = 0
    5.     NameFullyQualifiedDN = 1
    6.     NameSamCompatible = 2
    7.     NameDisplay = 3
    8.     NameUniqueId = 6
    9.     NameCanonical = 7
    10.     NameUserPrincipal = 8
    11.     NameCanonicalEx = 9
    12.     NameServicePrincipal = 10
    13. End Enum
    14.  
    15. Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" _
    16. (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    17.  
    18. Private Sub Command1_Click()
    19.     Dim sBuffer As String, Ret As Long
    20.     Dim current_user As String
    21.     sBuffer = String(256, 0)
    22.     Ret = Len(sBuffer)
    23.     If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
    24.          current_user = Replace(Left$(sBuffer, Ret) & " the great", Chr(0), vbNullString)
    25.     Else
    26.          current_user = Replace(Environ("USERNAME") & " the great", Chr(0), vbNullString)
    27.     End If
    28.     MsgBox current_user, vbOKOnly
    29. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get the Current user

    You rocks!!! thanks a bunch!!! it solved my problem

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get the Current user

    Np, glad to help.

    Ps, dont forget to Resolve your thread from the thread tools menu.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width