I spent most of the day searching for a solution to this, I'm starting to think its maybe not possible for my requirements
My basic setup is to run a vbscript (.vbs) called from an excel vba code. The vba code has to continue on and leave the vbscript running, but will monitor it from time to time using Exec.Status
In the vbscript I'm using WScript.StdOut.WriteLine "whatever"
to track/debug it's progress, but as it stands I can only read it's output after the excel vba code is finished what it needs to do.
What I want is to see a real time output to the console from the vbscript
Here's the vba code…
Dim WSH As IWshRuntimeLibrary.WshShell 'Windows Script Host Object Model
Dim Exec As WshExec
Set WSH = CreateObject("WScript.Shell")
Set Exec = WSH.Exec("%COMSPEC% /C CSCRIPT.EXE //nologo " _
& VbsFileDir _
& " " & Arg1 _
& " " & Arg2 _
& " " & Arg3 _
& " " & Arg4)
I have been able to get a real time output by converting from WSH.Exec
to WSH.Run
, but I do need the access to Exec.Status
, which is not available under WSH.Run
UPDATE – 2015-02-06
To clarify further… Using the example '…B.vbs' code provided by @Ekkehard.Horner's answer… The following excel-vba code WILL display a real-time output to the console…
WSH.Run("cscript C:\28353522-B.vbs")
…but the following WILL NOT display anything to the console
WSH.Exec("cscript C:\28353522-B.vbs")
I can't use the .Run()
because I use the .Status
flag from .Exec()
Also I can't just move the vbscript into the VBA code because the VBA goes on to do other tasks in parallel with the vbscript.
P.s. If anyone can submit an answer explaining why it can't be done, then I will mark that as accepted.
Best Answer
Use .Stdout.ReadLine() until the process has finished and .Stdout.ReadAll() to slurp the rest of the output - as in
28353522-A.vbs
28353522-B.vbs
output:
BTW - How did you get real-time output with .Run()?