2

I am running Gnuplot in the confines of VBA and it works surprisingly well. I am able to produce an entire book of about one hundred graphs using GNUPLOT and Word VBA. (With Excel graphs, I could get maybe about four)

There is one small problem - STDERR disappears, or is not being created, when run under VBA. No diagnostics.

Given the following small GNUPLOT script,

set key bmargin left horizontal Right noreverse enhanced autotitle box lt black linewidth 1.000 dashtype solid
set samples 800, 800
set title "Simple Plots" 
set title  font ",20" norotate
DEBUG_TERM_HTIC = 119
DEBUG_TERM_VTIC = 119
plot [-30:20] sin(x*20)*atan(x)

my VBA code writes it to a temporary file and attempts to run it. VBA generates

"C:\temp\Gnuplot\bin\gnuplot.exe" C:\Users\VVKOZLOV\AppData\Local\Temp\gnuA5E0.txt 2>"C:\Users\VVKOZLOV\AppData\Local\Temp\gnuA5E2.err"

Then runs it with the Windows Shell, invoked like so:

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")

then

wsh.Run x, 0, True ' x contains the generated GNUPLOT expression

GNUPLOT executes flawlessly.

In the GNUPLOT expression, the first item is the location of the GNUPLOT program, which on my system happens to be "C:\temp\Gnuplot\bin\gnuplot.exe". The second file is the temporary generated in the official Microsoft location C:\Users\VVKOZLOV\AppData\Local\Temp\gnuA5E0.txt. Last is the location of STDERR, 2>"C:\Users\VVKOZLOV\AppData\Local\Temp\gnuA5E2.err" where the 2> thing indicates that this is STDERR, not the normal output STDOUT.

Now suppose I introduce a deliberate error into the GNUPLOT source, I would expect an error file written containing something like

scooby doo where are you?
^
"C:\Users\VVKozlov\AppData\Local\Temp\gnuEC55.txt", line 4: invalid command

When executed with the usual CMD.EXE outside of VBA, the STDERR file is created as expected.

When executed in the confines of VBA using the Windows Shell, no STDERR file is created.

How could I get this STDERR file to be produced under VBA?

V. V. Kozlov
  • 189
  • 2
  • 10
  • Wrap the whole command line like `CmdLine = "CMD /S/C """ & CmdLine & """"`. These are magic words. – Ben Dec 10 '17 at 11:45
  • See also https://stackoverflow.com/questions/2075886/capturing-output-from-wshshell-exec-using-windows-script-host/9063149#9063149 – Ben Dec 10 '17 at 11:46
  • Thanks, Ben. It's those magic extra quotes. – V. V. Kozlov Dec 10 '17 at 12:28
  • At the insistence of several of my colleagues, I have added a version of this very work, but as an Excel addin, to [SourceForge](https://sourceforge.net/projects/gnuplot-add-in-for-excel/). – V. V. Kozlov Dec 29 '17 at 11:01

1 Answers1

5

CreateObject("WScript.Shell").Run executes a command line directly.

Output redirection (the 2> syntax you're using) is a feature of the cmd.exe Windows Command Processor.

What you want to do is either:

  1. Execute cmd.exe with the arguments and redirection you want.
  2. Use CreateObject("WScript.Shell").Exec instead, which gives you access to the input, output, and error streams directly. You would then need to read from the stream and write the contents to a file yourself within the script if you want them recorded to a file.
  • Variant 2 works out well for me. I don't need to explicitly state where to send STDERR output. Also, the error message comes to me as a string without having to explicitly create and read another file. Now to get rid of that annoying screen flash. – V. V. Kozlov Dec 09 '17 at 16:29
  • In the end, I used Variant 1. That worked with very few changes to the original code I wrote. – V. V. Kozlov Dec 18 '17 at 10:09