Search code examples
excelvbashellautoit

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.


Solution

  • 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