如何使用 vba 的 shell() 运行带有参数的 .exe? [英] How do you run a .exe with parameters using vba's shell()?

查看:81
本文介绍了如何使用 vba 的 shell() 运行带有参数的 .exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标文件路径,其结构类似于下面的示例.

I have a target file path that is structured like example below.

C:Program FilesTestfoobar.exe /G

我需要做的是能够使用 VBA 的 shell() 命令来执行这个文件.

What I need to do is be able to execute this file using VBA's shell() command.

我如何格式化文件路径以告诉 Shell() 有一个参数需要在运行 .exe 时调用

How do I have to format the file path to tell Shell() that there is an argument that it needs to call along with running the .exe

我读过/尝试过的(无济于事)如下,结果在右侧.

What I've read/tried (with no avail) is below with the results to the right.

file = """C:Program FilesTestfoobar.exe"" /G"    <---Bad file name or number (Error 52) 
shell(file)

file2 = "C:Program FilesTestfoobar.exe /G"       <---file never found
shell(file2)

我已经成功地使用 shell() 运行了其他 .exe,所以我知道这不是 VBA 或函数的问题.

I've succeeded with running other .exe's using shell() so I know it's not a problem with VBA or the function.

示例:

works = "C:Program FilesTest	est.exe"
shell(works)

我对执行需要附加参数的文件所涉及的过程不是特别熟悉,所以如果我说错了或者您需要更多信息,请告诉我.

I'm not particularly familiar with the process involved with executing files that require additional parameters so if I misspeak or you need more information, please let me know.

推荐答案

这对我有用(Excel 2013):

This works for me (Excel 2013):

Public Sub StartExeWithArgument()
    Dim strProgramName As String
    Dim strArgument As String

    strProgramName = "C:Program FilesTestfoobar.exe"
    strArgument = "/G"

    Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub

从这里获得灵感 https://stackoverflow.com/a/3448682.

这篇关于如何使用 vba 的 shell() 运行带有参数的 .exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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