Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get logged on username in Excel VBA - not the account running Excel (using RunAs)

Tags:

shell

excel

vba

I have logged into my workstation with my normal domain credentials. We shall call this AccountA.

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.

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.

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?

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 RunAs to carry out my commands with the proper permissions. Below are some things I have tried, all returning AccountB (the account used via RunAs to launch Excel)

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.

' 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 " & 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 > 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: " & objNet.UserName
Set objNet = Nothing

MsgBox "Environ username is: " & Environ("USERNAME")

MsgBox "Application username is: " & Application.UserName    
MsgBox "Network username is: " & WindowsUserName

Dim strUser As String
strUser = ActiveDirectory.user()
MsgBox "Welcome back, " & strUser

CurrentWorkbenchUser = Environ("USERDOMAIN") & "\" & Environ("USERNAME")
MsgBox CurrentWorkbenchUser

Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",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:" & txtdomain & "\" & txtuser & " ""cmd.exe /k dir /s *.*""", vbNormalFocus)

End Sub

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.

like image 849
user3508511 Avatar asked May 14 '14 19:05

user3508511


People also ask

How do I capture a UserName in Excel?

Insert Windows Username in Excel Cell To do this, press Alt+ F11, go to VB editor, insert a new module & enter this code. Then open the worksheet, then insert this formula “=GetUserName()” and press “Enter” key. Now, the cell will execute the function and display the Windows logged in username in the cell.

How do I run VBA in debug mode?

One of the methods used to debug VBA code is by running the code. The shortcut key for the command is F5. Start by placing the cursor into the UserForm or Sub (macro) and then press F5 to run the sub. Please note that F5 will not work when running a sub that requires parameters to execute a function.

What is application UserName in VBA?

UserName Application Property in VBA is used to sets or returns the name of the user.


1 Answers

The basic approach is to execute the shell with a "run as" parameter.

  • have you taken a look at this
  • also look here

This is code ripped from the ms sight and is just here to make sure that the next guy or gal who comes by has a quick reference.

Sub RegisterFile(ByVal sFileName As String)
ShellExecute 0, "runas", "cmd", "/c regsvr32 /s " & """" & sFileName & """", "C:\", 0 'SW_HIDE =0
End Sub

As an aside if you only need the account for access to a SQL server, you should be able to just set the account within the connection string in your vba MACRO. I've done that for an Oracle DB in the past.

like image 91
Semicolons and Duct Tape Avatar answered Oct 08 '22 11:10

Semicolons and Duct Tape