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!!!
Printable View
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!!!
If you mean vba coding - have a look at environ$ function. Usually stored in there somewhere.
The current user name is stored in;
VB Code:
Application.UserName
Many methods and code examples posted here - http://vbforums.com/showthread.php?t=357723
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!!
I cover the environ and well as other methods of obtaining the current windows logged on user in my faq code link. ;)
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!!!!
Sure, just sounds like something is incorrect. Post your code. ;)
here's the code..
VB Code:
Private Enum EXTENDED_NAME_FORMAT NameUnknown = 0 NameFullyQualifiedDN = 1 NameSamCompatible = 2 NameDisplay = 3 NameUniqueId = 6 NameCanonical = 7 NameUserPrincipal = 8 NameCanonicalEx = 9 NameServicePrincipal = 10 End Enum Private Declare Function GetUserNameEx Lib "secur32.dll" Alias _ "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, _ ByVal lpNameBuffer As String, ByRef nSize As Long) As Long Private Sub Command1_Click() Dim sBuffer As String, Ret As Long sBuffer = String(256, 0) Ret = Len(sBuffer) If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then current_user = Left$(sBuffer, Ret) & " the great" Else current_user = Environ("USERNAME") & " the great" End If msgbox current_user,vbOkOnly 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".
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:
Option Explicit Private Enum EXTENDED_NAME_FORMAT NameUnknown = 0 NameFullyQualifiedDN = 1 NameSamCompatible = 2 NameDisplay = 3 NameUniqueId = 6 NameCanonical = 7 NameUserPrincipal = 8 NameCanonicalEx = 9 NameServicePrincipal = 10 End Enum Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" _ (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long Private Sub Command1_Click() Dim sBuffer As String, Ret As Long Dim current_user As String sBuffer = String(256, 0) Ret = Len(sBuffer) If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then current_user = Replace(Left$(sBuffer, Ret) & " the great", Chr(0), vbNullString) Else current_user = Replace(Environ("USERNAME") & " the great", Chr(0), vbNullString) End If MsgBox current_user, vbOKOnly End Sub
You rocks!!! thanks a bunch!!! it solved my problem
Np, glad to help. :)
Ps, dont forget to Resolve your thread from the thread tools menu. ;)