VBA:运行“提升”命令(Shell 与 ShellExecute)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24741940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA: Running "Elevated" Command (Shell vs. ShellExecute)
提问by Shawn V. Wilson
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:
在我的 VBA 程序中,我需要运行应用程序“Skitch”并使用它打开一个 JPEG 文件。这是我一直在使用的命令:
ReturnValue = Shell("C:\Program Files (x86)\Evernote\Skitch\Skitch.exe " & """" & aPic & """", 1)
...where "aPic" is the path and filename.
...其中“aPic”是路径和文件名。
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?
经过一些试验,我想我需要像在提升的命令窗口中一样运行命令(换句话说,以“管理员身份”运行它)。是否可以提升运行 Shell?
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.)
如果这是不可能的:如果我理解正确,使用 ShellExecute 而不是 Shell 将自动提升命令。但我对它不太熟悉。有人可以告诉我如何使用 ShellExecute 运行我的命令吗?(顺便说一句,我知道 ShellExecute 适合运行与文件类型相关的命令,但在此用户的计算机上 *.jpg 可能不会与 Skitch 相关联。)
Thanks.
谢谢。
采纳答案by djikay
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.
我没有skitch
所以不能尝试这个,但它应该工作。
For more information about ShellExecute
, click hereto have a look on MSDN.
有关 的更多信息ShellExecute
,请单击此处查看 MSDN。