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

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

问题描述

  C:\Program Files\Test\\ \\foobar.exe / G 

我需要做的是能够使用vba的 shell()命令。



如何格式化文件路径来告诉 Shell ()有一个参数,它需要调用与运行.exe



我读过/尝试(没有下面的结果是正确的。

  file =C:\Program Files\Test\\ \\foobar.exe/ G<---文件名或号码错误(错误52)
shell(文件)

file2 =C:\Program Files\ Test\foobar.exe / G<---文件从未找到
shell(file2)

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



示例:

  works =C:\Program Files\Test\test.exe
shell(works)

我不太熟悉执行需要其他参数的文件所涉及的过程,所以如果我想错过或需要更多信息,请让我知道。 >

解决方案

这适用于我(Excel 2013):

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

strProgramName =C:\Program Files\Test\foobar。 exe
strArgument =/ G

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

从这里启发 https://stackoverflow.com/a/3448682


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

C:\Program Files\Test\foobar.exe /G

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

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 Files\Test\foobar.exe"" /G"    <---Bad file name or number (Error 52) 
shell(file)

file2 = "C:\Program Files\Test\foobar.exe /G"       <---file never found
shell(file2)

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

Example:

works = "C:\Program Files\Test\test.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.

解决方案

This works for me (Excel 2013):

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

    strProgramName = "C:\Program Files\Test\foobar.exe"
    strArgument = "/G"

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

With inspiration from here https://stackoverflow.com/a/3448682.

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

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