26

I am getting the 800A0414 error in lines 7 and 12 of this script:

Module Module1

  Dim p

    Sub Main()
        CreateObject("Wscript.Shell").Run("program.bat", 0, True)

        p = Process.GetProcessesByName("program")
        If p.Count > 0 Then
            WScript.Sleep(300000)
        Else
            CreateObject("Wscript.Shell").Run("program clean up.bat", 0, True)
        End If

    End Sub

    Private Function WScript() As Object
        Throw New NotImplementedException
    End Function

End Module

I am trying to run a batch script, that starts a process, then wait until the process terminates, then run another batch script. I also do not want any command boxes being shown. If their is a easier way please let me know.

Thanks for your help

Strong
  • 263
  • 1
  • 3
  • 6

3 Answers3

55

When you enclose a procedure's argument list in parentheses, you must use the Call keyword:

Call CreateObject("WScript.Shell").Run("program.bat", 0, True)

If you omit the Call keyword, you must also drop parentheses:

CreateObject("WScript.Shell").Run "program.bat", 0, True
Helen
  • 87,344
  • 17
  • 243
  • 314
  • 3
    There seems to be an exception that it will let you use parenthesis if you only use the first parameter. ie. `CreateObject("Wscript.Shell").Run("program.bat")` works without complaint – BeowulfNode42 Dec 08 '15 at 07:55
  • 1
    @BeowulfNode42: `.Run("program.bat")` is valid syntax - it's interpreted as `Call ...Run( ("program.bat") )`. Putting parentheses around a specific argument force this argument to be passed `ByVal` instead of `ByRef`. In other words, `MySub(param)` will work if the sub is expecting `ByVal` arguments, but won't work if it's expecting `ByRef` arguments. For details, see [ByRef and ByVal in VBScript](http://stackoverflow.com/q/1537819/113116). – Helen Dec 08 '15 at 14:41
12

To complete what's been said before:

When Call keyword is used to call a procedure (i.e. sub or function) the arguments must be enclosed in parentheses, except when the procedure has no arguments in which case the parentheses are optional. For example all the statements:

Call test()
Call test
Call test(1,2)

are valid, but not this one:

Call test 1

When calling a procedure without using the Call keyword, the parentheses can only be used when either the procedure has zero or one argument or the procedure has a return value (i.e. is a function) and its value is used in the same statement. For example all the statements:

test()
test(1)
test(1,2)
a = test
a = test(1,2)
a = test(test(1,2),2)

are valid, except the third one which has more than one argument. In case it's not clear, the inner call of "test" in the last statement is valid because its return value is used as an argument to another call.

Note that whenever parentheses is used in this text, it is meant to imply the possible comma-separated values as well.

Community
  • 1
  • 1
  • 1
    Note: `test(1)` is not the same as `test 1`. It's equivalent to `Call test( (1) )`, where the parentheses around the argument (not the argument list) force the argument to be passed `ByVal` instead of `ByRef`. See [ByRef and ByVal in VBScript](http://stackoverflow.com/q/1537819/113116) for details. – Helen Oct 14 '14 at 13:38
2

Seems to me this is a VB.NET, not VBScript code. You have Shell function in VB.NET (and other methods).

Anyway, Run returns any error code returned by the program, and if you store that result in a variable, you can use parentheses in this case.

Dim lResult As Long
lResult = CreateObject("Wscript.Shell").Run("program.bat", 0, True)

The rest was answered by @Helen.

Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28