Alberto Vitoriano
Alberto Vitoriano

Reputation: 101

Execute AutoIt and wait for finish

I'm needing to implement a macro that runs after autoit and finished running the program it runs the rest of the macro. I tried the Shellandwait(), but I did not find documentation explaining about it.

I took other examples of code that forum and got this:

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

When I run it returns me this error:

"Run-time error '-2147024894 (80070002)': Method 'Run' of object 'IWshShell3' failed"

I do not know what it means and I have no idea solution.

Upvotes: 5

Views: 2004

Answers (2)

robertocm
robertocm

Reputation: 132

Let me share two examples of VBA code for capturing the output stream from the AutoIt script (reads from the STDOUT stream)

VBA code:

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)

This is the AutoIt script for the first test (test0.au3):

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

Another option i'm finding useful is this vba class:

VBA Run Application - Capture Output. Author: dmaruca. Last updated 2012

Here an example:

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

And the script for testing (test.au3):

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

Upvotes: 1

SierraOscar
SierraOscar

Reputation: 17647

If xPath has any spaces in it you will need to wrap the expression in quotes.

Try something like this instead:

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

Upvotes: 2

Related Questions