VBA: Running "Elevated" Command (Shell vs. ShellExecute)

Shawn V. Wilson picture Shawn V. Wilson · Jul 14, 2014 · Viewed 13.2k times · Source

In my VBA procedure, I need to run the app "Skitch" and use it to open a JPEG file. This is the command I've been using:

ReturnValue = Shell("C:\Program Files (x86)\Evernote\Skitch\Skitch.exe " & """" & aPic & """", 1)

...where "aPic" is the path and filename.

After some experimenting, I think I need to run the command as if it were in an Elevated Command window (in other words, run it "as Administrator"). Is it possible to run Shell elevated?

If that's not possible: If I understand correctly, using ShellExecute instead of Shell will automatically elevate the command. But I'm much less familiar with it. Can someone show me how to run my command using ShellExecute? (BTW, I know that ShellExecute is good for running commands associated with the file type, but on this user's computer *.jpg will likely not be associated with Skitch.)

Thanks.

Answer

djikay picture djikay · Jul 14, 2014

Try this:

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

Const SW_SHOWNORMAL = 1

Public Sub test()

  ShellExecute 0, "runas", "C:\Program Files (x86)\Evernote\Skitch\Skitch.exe", aPic, vbNullString, SW_SHOWNORMAL

End Sub

I don't have skitch so can't try this, but it should work.

For more information about ShellExecute, click here to have a look on MSDN.