|
-
Jun 6th, 2006, 04:13 AM
#1
Thread Starter
Fanatic Member
[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!!!
-
Jun 6th, 2006, 06:01 AM
#2
Re: Get the Current user
If you mean vba coding - have a look at environ$ function. Usually stored in there somewhere.
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...
-
Jun 6th, 2006, 08:45 AM
#3
Re: Get the Current user
The current user name is stored in;
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Jun 6th, 2006, 09:06 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 6th, 2006, 06:30 PM
#5
Thread Starter
Fanatic Member
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!!
-
Jun 6th, 2006, 06:57 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 6th, 2006, 07:32 PM
#7
Thread Starter
Fanatic Member
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!!!!
-
Jun 7th, 2006, 12:57 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 7th, 2006, 01:21 AM
#9
Thread Starter
Fanatic Member
Re: Get the Current user
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".
-
Jun 7th, 2006, 01:37 AM
#10
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:
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 7th, 2006, 03:07 AM
#11
Thread Starter
Fanatic Member
Re: Get the Current user
You rocks!!! thanks a bunch!!! it solved my problem
-
Jun 7th, 2006, 03:11 AM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|