2

I'm shelling to a batch file from VBA, to launch an exec file, such as Notepad.exe. I would like to return an indication or message back to VBA if the exec file wasn't found. So far, I've been doing this by having the bat write a message to a text file, then have VBA examine this file. This approach seems a bit kludgy, but so far I haven't come across an alternate method.

@echo off

set EM="C:\Msg.txt"
if exist %EM% del %EM%

set FL=%SystemRoot%\system32\xnotepad.exe

if not exist %FL% (
echo %FL% not found > %EM%
goto done
)

Start "" %FL%

:done

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but no matter what I set somenumber to, exitcode always remains at 0. I must be doing something wrong, will look at this some more tomorrow.
I'm using your VBA code verbetim (but without usr, pass params) with a test bat file that contains 1 line exit /b 3. But no matter what I try, exitcode is always = 0, so the error code is not being returned. Any suggestions?
OK, I'm using both your VBA and bat code exactly as you have it (copied & pasted), just changed the name of the bat file to match mine. Still no dice, just returns 0. Interestingly, if I change the bat file name to cmd.exe, then manually type exit /b 3 from the command line, the exitcode works as expected. I'm running this on Win XP, if that makes any difference (shouldn't). Thanks for you help and patience.
Well, I tried it on both of my Win XP systems, didn't work. Borrowed a friend's Win 7 laptop, and presto - it worked! So my problem must be a Win XP issue. Will let you know if it gets resolved, thanks.
Have a look at THIS, seems we need to use exit # with WinXP, and not exit /b #. Don't completely understand why, but it seems to work for me.
-1

You can do this with the EXIT command. Take a look at EXIT /?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.