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

Mofi
  • 46,139
  • 17
  • 80
  • 143
user3647205
  • 63
  • 1
  • 1
  • 5
  • 1
    What is your batch file code? This VBA runs the following batch file code perfectly for me: `net use` `pause` – u8it Dec 31 '15 at 14:23
  • 1
    Here is [a similar post](http://stackoverflow.com/questions/21267283/error-running-shell-object-commands-through-excel-vba) (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 – u8it Dec 31 '15 at 14:38
  • 2
    Try `wsh.Run("cmd.exe /C D:\test.bat", windowStyle, waitOnReturn)` – xmojmr Jan 01 '16 at 07: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. – user3647205 Jan 01 '16 at 09:03
  • @user3647205 probably not unless `D:\myfolder\make.exe` exists – xmojmr Jan 01 '16 at 09:07

0 Answers0