4

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.

Shawn V. Wilson
  • 1,002
  • 3
  • 17
  • 42

1 Answers1

5

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.

djikay
  • 10,450
  • 8
  • 41
  • 52
  • I was able to use this method to call REG.EXE to ADD (modify) a registry entry that normally requires Admin rights. This will prompt for elevated rights, which is what I want the user to see just for one operation (toggling *TraceSQLMode* for ODBC). Example: `strParameters = """HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Wow6432Node\Microsoft\Office\16.0\Access Connectivity Engine\Engines\ODBC""" ; strParameters = "ADD " & strParameters & " /v TraceSQLMode /t REG_DWORD /d " & varValue & " /f" ; ShellExecute 0, "runas", "REG.EXE", strParameters, vbNullString, 0 ` – Ben Apr 08 '20 at 04:36