<p>I'm working on a spec for a piece of software for my company and as part of the auditing system I think it would be neat if there was a way to grab the current Active Directory user.</p>

<p>Hopefully something like:</p>

<pre><code>Dim strUser as String
strUser = ActiveDirectory.User()
MsgBox "Welcome back, " &amp; strUser
</code></pre>

[{'answer_id': 9054, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://bytes.com/forum/thread717576.html" rel="nofollow noreferrer">Try this article</a> - I have some code at work that will erm, work if this doesn\'t...</p>\n\n<p>Relevant quote:</p>\n\n\n\n<blockquote>\n<pre class="lang-vba prettyprint-override"><code>Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _\n                    (ByVal IpBuffer As String, nSize As Long) As Long\nPrivate Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _\n                    (ByVal lpBuffer As String, nSize As Long) As Long\n\nFunction ThisUserName() As String\n    Dim LngBufLen As Long\n    Dim strUser As String\n\n    strUser = String$(15, " ")\n    LngBufLen = 15\n\n    If GetUserName(strUser, LngBufLen) = 1 Then\n        ThisUserName = Left(strUser, LngBufLen - 1)\n    Else\n        ThisUserName = "Unknown"\n    End If\nEnd Function\n\nFunction ThisComputerID() As String\n    Dim LngBufLen As Long\n    Dim strUser As String\n\n    strUser = String$(15, " ")\n    LngBufLen = 15\n\n    If GetComputerName(strUser, LngBufLen) = 1 Then\n        ThisComputerID = Left(strUser, LngBufLen)\n    Else\n        ThisComputerID = 0\n    End If\nEnd Function\n</code></pre>\n</blockquote>\n'}, {'answer_id': 67563, 'author': 'David-W-Fenton', 'author_id': 9787, 'author_profile': 'https://Stackoverflow.com/users/9787', 'pm_score': 2, 'selected': False, 'text': '<p>Depending on environment variables to remain valid is a bad idea, since they can easily be changed within a user session.</p>\n'}, {'answer_id': 249759, 'author': 'Yarik', 'author_id': 31415, 'author_profile': 'https://Stackoverflow.com/users/31415', 'pm_score': 2, 'selected': False, 'text': '<p>David made a very good point about risk of using environment variables. I can only add that there may be other problems with environment variables. Just look at this actual code fragment from our 5-year old project:</p>\n\n<pre class="lang-vb prettyprint-override"><code>Public Function CurrentWorkbenchUser() As String\n\n    \' 2004-01-05, YM: Using Application.CurrentUser for identification of \n    \' current user is very problematic (more specifically, extremely \n    \' cumbersome to set up and administer for all users). \n    \' Therefore, as a quick fix, let\'s use the OS-level user\'s \n    \' identity instead (NB: the environment variables used below must work fine\n    \' on Windows NT/2000/2003 but may not work on Windows 98/ME)\n    \' CurrentWorkbenchUser = Application.CurrentUser\n    \'\n    \' 2005-06-13, YM: Environment variables do not work in Windows 2003. \n    \' Use Windows Scripting Host (WSH) Networking object instead.\n    \' CurrentWorkbenchUser = Environ("UserDomain") &amp; "\\" &amp; Environ("UserName")\n    \'\n    \' 2007-01-23, YM: Somewhere between 2007-01-09 and 2007-01-20, \n    \' the WshNetwork object stopped working on CONTROLLER3. \n    \' We could not find any easy way to fix that.\n    \' At the same time, it turns out that environment variables \n    \' do work on Windows 2003.\n    \' (Apparently, it was some weird configuration problem back in 2005: \n    \' we had only one Windows 2003 computer at that time and it was \n    \' Will\'s workstation).\n    \'\n    \' In any case, at the time of this writing, \n    \' returning to environment variables \n    \' appears to be the simplest solution to the problem on CONTROLLER3.\n    \' Dim wshn As New WshNetwork\n    \' CurrentWorkbenchUser = wshn.UserDomain &amp; "\\" &amp; wshn.UserName\n\n    CurrentWorkbenchUser = Environ("USERDOMAIN") &amp; "\\" &amp; Environ("USERNAME")\n\nEnd Function\n</code></pre>\n'}, {'answer_id': 31496094, 'author': 'bgmCoder', 'author_id': 1038866, 'author_profile': 'https://Stackoverflow.com/users/1038866', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s my version: it will fetch anything you like:</p>\n\n<pre class="lang-vb prettyprint-override"><code>\'gets firstname, lastname, fullname or username\nPublic Function GetUser(Optional whatpart = "username")\n    Dim returnthis As String\n    If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function\n    Set objSysInfo = CreateObject("ADSystemInfo")\n    Set objUser = GetObject("LDAP://" &amp; objSysInfo.USERNAME)\n    Select Case whatpart\n        Case "fullname": returnthis = objUser.FullName\n        Case "firstname", "givenname": returnthis = objUser.givenName\n        Case "lastname": returnthis = objUser.LastName\n        Case Else: returnthis = Environ("USERNAME")\n    End Select\n    GetUser = returnthis\nEnd Function\n</code></pre>\n\n<p><a href="http://community.spiceworks.com/scripts/show/425-get-the-current-logged-on-user-s-first-last-name" rel="nofollow noreferrer">I got the original idea from Spiceworks</a>.</p>\n'}]
['https://Stackoverflow.com/questions/9052', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428190/']