in VBA,
Dim oSHELL, batchname, usr, pass, exitcode
Set oSHELL = VBA.CreateObject("WScript.Shell")
usr="username"
pass="password"
batchname="batchFile.bat"
' Arguments ToRun, Style (0=hide), Waitforend
exitcode = oSHELL.Run(""""+batchname+""" """+usr+""" """+pass+"""", 0, True)
and in your batch
exit somenumber
should return somenumber to exitcode
Actual code I used:
Sub q27097252()
Dim oSHELL, batchname, usr, pass, exitcode
Set oSHELL = VBA.CreateObject("WScript.Shell")
usr = ""
pass = ""
batchname = "c:\106x\q27097252.bat"
' Arguments ToRun, Style (0=hide), Waitforend
exitcode = oSHELL.Run("""" + batchname + """ """ + usr + """ """ + pass + """", 0, True)
MsgBox (exitcode)
End Sub
With batch c:\106x\q27097252.bat
@ECHO OFF
SETLOCAL
EXIT %time:~-1%
GOTO :EOF
Ran perfectly well for me in VBA code editor/F5 (expected result: messagebox showing 0..9 at random)
Following comment/solution when using Windows XP:
It would appear the exit /b number option simply sets errorlevel whereas exit number actually sets the termination code.
Termination code is 0 under XP since cmd.exe actually terminated normally — whereas Windows 7 (and later) appears to assign the current errorlevel as exit code for the cmd.exe process.
Hence, use exit number by preference for compatibility with XP — code adjusted to suit.