Search code examples
excelvbabatch-fileprocedure

Getting "Invalid Procedure Call Argument" When i run call a Bat file from Subroutine


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"


Solution

  • 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