VBA:运行“高架"命令(Shell 与 ShellExecute) [英] VBA: Running "Elevated" Command (Shell vs. ShellExecute)

查看:76
本文介绍了VBA:运行“高架"命令(Shell 与 ShellExecute)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 VBA 程序中,我需要运行应用程序Skitch"并使用它打开一个 JPEG 文件.这是我一直在使用的命令:

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)EvernoteSkitchSkitch.exe " & """" & aPic & """", 1)

...其中aPic"是路径和文件名.

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

经过一些试验,我认为我需要像在提升的命令窗口中一样运行该命令(换句话说,以管理员身份"运行它).是否可以运行提升的 Shell?

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?

如果那不可能:如果我理解正确,使用 ShellExecute 而不是 Shell 将自动提升命令.但我对它不太熟悉.有人可以告诉我如何使用 ShellExecute 运行我的命令吗?(顺便说一句,我知道 ShellExecute 适合运行与文件类型相关的命令,但在此用户的计算机上 *.jpg 可能不会与 Skitch 相关联.)

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.)

谢谢.

推荐答案

试试这个:

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)EvernoteSkitchSkitch.exe", aPic, vbNullString, SW_SHOWNORMAL

End Sub

我没有 skitch,所以无法尝试,但应该可以.

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

有关 ShellExecute 的更多信息,点击这里查看MSDN.

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

这篇关于VBA:运行“高架"命令(Shell 与 ShellExecute)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆