执行AutoIt并等待完成

时间:2015-11-19 16:00:12

标签: excel vba shell autoit

我需要实现一个在autoit之后运行并运行完程序的宏,它运行宏的其余部分。 我尝试了Shellandwait(),但我没有找到解释它的文档。

我拿了论坛的其他代码示例并得到了这个:

Sub autoit()
Dim hProcess As Long
Dim xPath As String
Dim wsh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

Set wsh = CreateObject("WScript.Shell")

xPath = Application.ActiveWorkbook.Path

hProcess = wsh.Run("D:\Program Files\autoit-v3\install\AutoIt3_x64.exe " _
& xPath & "\leandro.au3", 0, True)

Workbooks.Open (xPath & "\Mudança " & Format(Date, "dd_mm_yyyy") & ".csv")

End Sub

当我运行它时返回我的错误:

  

"运行时错误' -2147024894(80070002)':方法'运行'对象' IWshShell3'失败"

我不知道这意味着什么,我不知道解决方案。

2 个答案:

答案 0 :(得分:2)

如果xPath中有任何空格,则需要用引号括起表达式。

尝试这样的事情:

xPath = ActiveWorkbook.Path

With CreateObject("WSCript.Shell")
    .Exec "CMD /C START /WAIT ""D:\Program Files\autoit-v3\install\AutoIt3_x64.exe"" """ & xPath & "\leandro.au3"""
End With

答案 1 :(得分:0)

让我分享两个用于从AutoIt脚本捕获输出流(从STDOUT流中读取)的VBA代码示例

VBA代码:

Dim sFilePath As String
Dim objShell As Object, objCmdExec
sFilePath = String(1, 34) & Application.ActiveWorkbook.path & Application.PathSeparator & "test0.au3" & String(1, 34)

Set objShell = CreateObject("WScript.Shell")
Set objCmdExec = objShell.exec("""C:\Program Files (x86)\AutoIt3\AutoIt3.exe"" " & sFilePath)

MsgBox (objCmdExec.StdOut.ReadAll)

这是第一个测试(test0.au3)的AutoIt脚本:

Global $iPID = Run("notepad.exe", "", @SW_SHOWMAXIMIZED)
WinWait("[CLASS:Notepad]", "", 10)
Sleep(2000)
Send("Hello")
Sleep(1000)
ProcessClose($iPID)
ConsoleWrite("Done" & @CRLF)

我发现另一个有用的选择是此vba类:

VBA Run Application - Capture Output。作者:dmaruca。上次更新时间2012年

这里有个例子:

Dim sFilePath As String
sFilePath = ThisWorkbook.path & Application.PathSeparator & "test.au3"

Dim RunApp As New CRunApp
RunApp.Command = "C:\Program Files (x86)\AutoIt3\AutoIt3.exe"
RunApp.AddParamater sFilePath
RunApp.AddParamater "Buenos días"
MsgBox RunApp.RunAppWait_CaptureOutput()

以及用于测试的脚本(test.au3):

MsgBox(4096, 'AutoIt MsgBox', $CmdLine[1])
ConsoleWrite($CmdLine[1] & @CRLF & "From: " & @ScriptName)