3

I am using the following VBA code to run a bat file. It was an example to run the note pad application instead of .bat file. While running I am getting an error "Method run of object ISshShell3 failed" Please let me know how can i use it to run the bat file from a desired directory? My aim is to run a command on console and wait for the command to finish.

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 Long
errorCode = wsh.Run("D:\test.bat", windowStyle, waitOnReturn)
If errorCode = 0 Then
 MsgBox "Done! No error to report."
Else
  MsgBox "Program exited with error code " & errorCode & "."
End If

Thanks in advance

5
  • 1
    What is your batch file code? This VBA runs the following batch file code perfectly for me: net use pause Commented Dec 31, 2015 at 14:23
  • 1
    Here is a similar post (is "IWshShell3" a mistype of "ISshShell3"? This is the only Google result for "ISshShell3"). If so, it looks like the error may be resolved by removing spaces from your directory (apparently not a problem with "D:\test.bat", or by setting wsh.CurrentDirectory = exePath... I can't test this because I still can't replicate the error, even when running a batch file from a flash drive or network folder... which makes me think your Windows or XL version may be relevant Commented Dec 31, 2015 at 14:38
  • 2
    Try wsh.Run("cmd.exe /C D:\test.bat", windowStyle, waitOnReturn) Commented Jan 1, 2016 at 7:52
  • 1
    thank you very much I tried wsh.Run("cmd.exe /C D:\test.bat", windowStyle, waitOnReturn). It is working fine. Please let me know can we use the above command to run the command directly. For eg : D:\myfolder\make (without using a bat file). My aim is to run a command and wait till it finishes execution. And I want to know the exit status also. Commented Jan 1, 2016 at 9:03
  • @user3647205 probably not unless D:\myfolder\make.exe exists Commented Jan 1, 2016 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.