1

enter image description here

Find the below code:

Sub Button1_Click()
   argh = Shell("C:\Users\YYYY\Desktop\test1.bat", vbNormalNoFocus)
End Sub

When I run call a Bat file from Subroutine, I always get below error. Attached screen shot as well.

"Run time Error"
"Invalid Procedure call or argument"

braX
  • 11,506
  • 5
  • 20
  • 33
  • Possible duplicate of [Shell cmd in access vba returns error 5 - invalid procedure call or argument](https://stackoverflow.com/q/44894706/11683) – GSerg Oct 22 '18 at 11:49
  • Without a [mcve], I don't think that it is possible to answer your question. You could explore using VBScript's `Run` or `Exec` methods as an alternative to `Shell`. It is [possible to use it from VBA](https://stackoverflow.com/q/26762566/4996248) – John Coleman Oct 22 '18 at 11:58

1 Answers1

1

We cannot run a .bat file from Excel macro , It will always ask for the Administrator permissions.

Better Create a .vbs file and call the .bat file from .vbs file.

.vbs file : Save the file as runbatch.vbs

Set objShell = CreateObject("WScript.Shell")
objShell.run "C:\Users\YYYYYY\Desktop\test1.bat"
WScript.Quit

Macro Code:In Excel VB Editor Write the below code

Sub Button1_Click()
    shell "wscript C:\Users\YYYYYY\Desktop\runbatch.vbs", 
    vbNormalFocus
End Sub