【问题标题】:vba WScript.Shell run .exe file with parametervba WScript.Shell 运行带有参数的 .exe 文件
【发布时间】:2015-01-01 23:04:54
【问题描述】:

我正在从 Word 2013 运行 VBA 宏。我正在尝试运行需要文件名参数/参数的可执行文件。例如c:\myfilefilter.exe filetobefiltered.htm

我想使用Shell Run,因为我希望 VBA 代码等到可执行文件完成运行后再恢复 VBA 代码。下面代码中的最后三行是我尝试过的一些不起作用的语法示例。我认为问题在于可执行程序与其过滤的文件之间的空间。有谁知道正确的语法或等待可执行程序完成的方法。如果您需要更多信息,请询问。

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer
Dim strProgramName As String
Dim strMyFile As String
Call Shell("""" & strProgramName & """ """ & strMyFile & """", vbNormalFocus, waitOnReturn"""")
wsh.Run(strProgramName & """ """ & fileToFilterB & """", vbNormalFocus, waitOnReturn)
wsh.Run "Chr(34)strProgramNameChr(34)strMyFile", waitOnReturn

下面的代码有效。在strCMD之后,第一个数字是一个布尔参数:1显示dos框,0隐藏dos框;第二个数字类似:1 等待程序完成再继续 VBA 代码,0 不等待。

strCMD = sMyProgram + " " + sMyFile
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run strCMD, 1, 1

【问题讨论】:

  • 我不知道答案,也没有很好的测试方法,但是在最后一行中,您应该删除所有引号并将 Chr(34) 和您的变量与 & 号连接起来.我假设您在某处定义了所有这些变量?

标签: vba ms-word wsh


【解决方案1】:

你可以试试这个。对我有用。

Const BatchFileName = "P:\Export.bat"

将 wsh 调暗为对象

Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

wsh.Run BatchFileName, windowStyle, waitOnReturn

Kill BatchFileName

【讨论】:

    【解决方案2】:

    这对我有用(来自 MsAccess 2013 VBA)

    Dim wShell As New WshShell
    Dim wsExec As WshExec
        Dim cmdline As String
        cmdline = "notepad c:\somefile.txt"
        Debug.Print Now, cmdline
        Set wsExec = wShell.Exec(cmdline)
        Do While wsExec.Status = 0
            DoEvents
        Loop
        Debug.Print Now, "done"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-21
      • 2015-05-18
      • 2014-05-31
      • 2011-10-23
      • 2013-04-02
      • 2018-09-27
      • 1970-01-01
      相关资源
      最近更新 更多