0

If I'm using Visual Basic to run an executable using the Shell() command, how would I run that executable as an administrator? My method works, at least in practice, but one executable won't run properly and I think that's the issue.

Here's an example of the command I'm trying to run.

Shell("%temp%\ninite.exe")

Edit: Sorry, this is VB.net, not VBA. I put a bad tag on there.

  • Hard to guess what "not properly" could possibly mean. The legacy Shell() function doesn't do anything at all to ensure that a process runs with admin privileges. That requires using the Process class, runas verb. – Hans Passant Nov 12 '14 at 17:26
  • the question is would you need to run `Shell` as administrator or just the `.exe` ? –  Nov 12 '14 at 17:27
  • This might help: If I run my app as administrator, everything works fine. But I want this code to run for people who are computer illiterate and probably won't be able to run my app as an administrator. – Matthew Berg Nov 12 '14 at 18:50
  • And by "not properly" I mean not at all. File not found, even though it's there. – Matthew Berg Nov 12 '14 at 18:54

1 Answers1

0

To stay strictly in VBA with no .NET dependencies, you should be able to use the ShellExecute Win32 function.

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hWnd As Long, _
        ByVal lpOperation As String, 
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As Long 

You can pass "runas" to lpOperation instead of the more usual "open" (commonly called the verb). Note that this may cause a dialog box to come up prompting the user for credentials.

A better way might be to use the CreateProcess or CreateProcessAsUser function which is probably the most powerful way to launch anything in Windows, but that is certainly more complex and I cannot tell you the details of how to get it to work right.

Mike
  • 1,274
  • 10
  • 24