title: Get logged on username in Excel VBA - not the account running Excel (using RunAs)
body: <p>I have logged into my workstation with my normal domain credentials. We shall call this AccountA.</p>
  
  <p>I then use the "run as a different user" to launch Excel. We shall call this AccountB. I do this because the permissions needed to query some SQL servers must be done using AccountB. </p>
  
  <p>At this point, I need to include a subroutine to launch a Shell to create directories and move files on a remote server. AccountB doesn't have (and cannot have) permissions to do this. My shell gives me an Access Denied message. Fine. </p>
  
  <p>So now, I need to have VBA return the name of AccountA, the account I have used to log into the computer. How do I do this? </p>
  
  <p>I have seen quite a few examples on this site as well as others that will return the username running Excel (AccountB). However, I have not seen any examples that will pull AccountA information to pass to the Shell via <code>RunAs</code> to carry out my commands with the proper permissions. Below are some things I have tried, all returning AccountB (the account used via <code>RunAs</code> to launch Excel)</p>
  
  <p>This application will be used by multiple people with permissions to run cmd shell on the remote server, so AccountA cannot be hardcoded and must be programmatically obtained. </p>
  
  <pre class="lang-vb prettyprint-override"><code>' Access the GetUserNameA function in advapi32.dll and
  ' call the function GetUserName.
  Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
  (ByVal lpBuffer As String, nSize As Long) As Long
  
  Sub XXXXXXXXXX()
  '//
  ThisWorkbook.Sheets("XXXXXXXXXX").Activate ' select the worksheet for use
  cmdString = UCase(Trim(ThisWorkbook.ActiveSheet.Cells(4, 4).Value)) 'get XXXXXXXXXX
  
  'MsgBox cmdString
  'retval = Shell("cmd.exe /k " &amp; cmdString, vbNormalNoFocus)
  'cmdString = "MkDir \\XXXXXXXXXX\g$\Tempski" 'fails - no permission
  
  'Set the Domain
  txtdomain = "XXXXXXXXXX"
  'Acquire the currently logged on user account
  'txtuser = "XXXXXXXXXX"
  
   ' Dimension variables
   Dim lpBuff As String * 255
   Dim ret As Long
  
   ' Get the user name minus any trailing spaces found in the name.
   ret = GetUserName(lpBuff, 255)
  
   If ret &gt; 0 Then
   GetLogonName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
   Else
   GetLogonName = vbNullString
   End If
  
  MsgBox GetLogonName
  
  Dim objNet As Object
  On Error Resume Next
  Set objNet = CreateObject("WScript.NetWork")
  MsgBox "Network username is: " &amp; objNet.UserName
  Set objNet = Nothing
  
  MsgBox "Environ username is: " &amp; Environ("USERNAME")
  
  MsgBox "Application username is: " &amp; Application.UserName    
  MsgBox "Network username is: " &amp; WindowsUserName
  
  Dim strUser As String
  strUser = ActiveDirectory.user()
  MsgBox "Welcome back, " &amp; strUser
  
  CurrentWorkbenchUser = Environ("USERDOMAIN") &amp; "\" &amp; Environ("USERNAME")
  MsgBox CurrentWorkbenchUser
  
  Set WSHnet = CreateObject("WScript.Network")
  UserName = WSHnet.UserName
  UserDomain = WSHnet.UserDomain
  Set objUser = GetObject("WinNT://" &amp; UserDomain &amp; "/" &amp; UserName &amp; ",user")
  GetUserFullName = objUser.FullName
  
  MsgBox GetFullUserName
  
  '//try to pass user credentials to shell - service account cannot run XXXXXXXXXX
  '//This Works when txtdomain and txtuser are passed properly (MUST GRAB THESE SOMEHOW)
  'retval = Shell("runas /user:" &amp; txtdomain &amp; "\" &amp; txtuser &amp; " ""cmd.exe /k dir /s *.*""", vbNormalFocus)
  
  End Sub
  </code></pre>
  
  <p>All the bits of code above return the account used to launch Excel — not what I need i.e. the account I used to log into the computer.</p>