GNUPLOT vs. VBA - where did STDERR go? - vba

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?

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:
Execute cmd.exe with the arguments and redirection you want.
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.

Related

Environment Variables not recognized when calling Run in WScript.Shell object

If I run the command Rscript "C:/TEMP/test.R" in the command line it works and my script runs as expected. Once I try to run it in my VBA code it does not recognize the Rscript as a valid command.
Dim shell_obj As Object
Set shell_obj = VBA.CreateObject("WScript.Shell")
Dim errorCode As Integer
errorCode = shell_obj.Run("Rscript ""C:/TEMP/test.R""", 1, True)
When I looked into the PATH variable being used by the WScript.Shell I saw that it does not include the System Variables with the Rscript path inside of it.
Dim shell_obj As Object
Dim wshSystemEnv As Object
Set shell_obj = VBA.CreateObject("WScript.Shell")
' This one does not include the path to the Rscript'
Debug.Print shell_obj.ExpandEnvironmentStrings("%PATH%")
Set wshSystemEnv = shell_obj.Environment("SYSTEM")
' This one includes the path to the Rscript'
Debug.Print wshSystemEnv("PATH")
Can I force the the WScript.Shell object to use the System environment? Or at least use its variables?
Cmd:
VBA (version 1):
VBA (version 2):
EDIT: See bottom of post.
Hopefully you'll find some use in my (lengthy) take on this... :-)
Testing for command-line readiness
Any command (including an RScript) that can be run as-is from the Windows command-line can also be run with either the VBA Shell function or the Windows WScript.Shell method.
The issue is, your cmd string is not command-line ready. This can be confirmed by hitting +R and pasting the contents of your cmd string variable:
Rscript "**path**/test.R"
I don't currently have rscript.exe installed but I suspect you will get an error if you try running your command manually in either the Run window or on the command line. If it doesn't run there it obviously won't run with in a VBA Shell.
As I understand it, the double asterisk is a Java notation the way you are using it, and in R is the same as a ^ caret character, which is for calculating exponents.
Referencing an environment variable
To return the Windows PATH environment variable in VBA, you would use VBA's Environ function.
To insert the value environment variable inline at the command line, you would surround it with %percent% symbols, like %path%
Windows' PATH environment variable
PATH does not return a single folder. It's a list of folders that Windows should check to find an executable file that one attempts to run.
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given.
The Windows system directory (typically C:\WINDOWS\system32) is typically the first directory in the path, followed by many (but not all) of the directories for installed software packages.
An example a default value of PATH (from a fresh install of Windows 7) is:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
As with %path%, this includes%SystemRoot%` which, by default on Windows 7 is the string:
C:\Windows
Checking environment variables
You can verify the value of your PATH environment label:
Hit +R.
Type cmd and hit Enter. (A command line window should open.)
Type or paste echo %path% and hit Enter.
The contents of the Window PATH environment variable will be displayed.
You can also check environment variables from within Windows:
and type env (to search)
Click Edit the system environment variables. (There is a similar option "...for your account" which is not quite the same.)
Click
Note: Although you technically can change the PATH in this window, I would not recommend doing so, especially with PATH since it is split up into System and User folders, and Windows likes certain folders in certain areas, and some changes don't take effect until reboot but others do, and blah blah blah, trust me: it's easier to do from the command line.
What's wrong with your code?
Therefore it based on all of this, it appears that the command you're trying to run is:
Rscript "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem/test.R"
...which obviously will not work.
Get or set the current working folder/directory
I can only speculate as to what you're trying to accomplish.
I suspect you didn't intend to return the entire PATH variable, but are only interested in the current working folder.
If so, you don't need to specify a folder at all. Shell already commands execute in the "current" folder.
One way you can check which directory or folder is current, is with the VBA CurDir() function, like:
Debug.Print CurDir()
The value of CurDir can be changed with the ChDir statement.
Similar functions
Note that the CurDur() command is often confused with similar functions like:
Application.Path which returns the path to the Excel application, or,
ActiveWorkbook.Path which returns the location that the active workbook is saved (or an empty string if it's unsaved).
Possible Solution: (How to run an rScript in the current path in VBA)
If your R script and the rscript.exe are both in the current working folder, run it with just one line of VBA:
Shell "rscript.exe test.R", vbNormalFocus
If you require VBA to wait for execution of the Shell command to complete before resuming VBA, then you can you just this one line:
CreateObject("WScript.Shell").Run "rscript.exe test.R", vbNormalFocus, True
More Information:
I generally make a point of including links to any sites I used to verify my answers, so this must be my most-researched answer yet because I've never had a list this long... and I left some off out this time!
Stack Overflow : Running R scripts from VBA
MSDN : Shell Function (VBA)
MSDN : Environ Function (VBA)
R-Bloggers : Passing arguments to an R script from command lines
Stack Overflow : R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?
Stack Exchange: Statistics : Double star ** in R?
Wikipedia : PATH (variable)
Stack Overflow : Default values of PATH variable in Windows 10
SuperUser : Default PATH for Windows 7
MSDN : CurDir() Function (VBA)
MSDN : ChDir() Statement (VBA)
MSDN : Application.Path (Excel/VBA)
MSDN : ActiveWorkbook.Path (Excel/VBA)
Stack Overflow : Wait for Shell command to complete
MSDN Forums : Difference between wscript.shell and shell.application
Stack Overflow : Steps to run R script through Windows command prompt
One More Demo of What's Wrong With Your Code:
I have a batch file named test.bat located in C:\WINDOWS. My PATH environment variable contains C:\WINDOWS (among other things).
If I go to the command prompt in root folder C:\ and type test.bat:
...it runs properly (even though my file is not in that folder... since the c:\windows folder is within the PATH variable.)
However, if I go to the command prompt and type C:\test.bat:
...it does not work. It cannot find the file because I specified a folder where the file is not located.
--- In VBA, if I run the command Shell "test.bat",1:
...it runs properly (even though my file is not in that folder... since the c:\windows folder is within the PATH variable.)
However, if in VBA I run the command Shell "c:\test.bat",1:
...it does not work. It cannot find the file because Ispecified* a folder where the file is not located**.
Both VBA and the Shell command are behaving the same way, when given the same information.
If you have recently modified the system PATH variable, you must restart the Office application you are running VBA from. VBA sends WScript the path variable and only rereads it on a restart. On restart, it will reread the PATH variable from the system and send the new correct path to WScript.
I had the same issue. I had updated the system PATH variable, but the WScript.Shell object was being passed the path variable from Excel rather than reading from the system. Excel had read the path at startup and was not aware it had changed. Once I closed Excel and reopened, WScript had the updated path variable and my script execute successfully.

using environment variables in excel

So I am using this code in excel to read environment parameters on startup:
Dim ExcelArgs As String
Dim arg As String
ExcelArgs = Environ("ExcelArgs")
MsgBox ExcelArgs
If InStr(UCase(ExcelArgs), "CREO") >= 0 Then
Application.DisplayAlerts = False
If Len(ExcelArgs) > Len("CREO") Then
arg = Split(ExcelArgs, ",")(1)
Call Creo.addNewPartToPartslist(arg)
End If
Application.DisplayAlerts = True
End If
and this line in my batch script:
echo "Launch excel"
Set "ExcelArgs=CREO,DXFWITHOUTDRW
"C:\Program Files (x86)\Microsoft Office\OFFICE16\Excel.exe" /r "%APPDATA%\Microsoft\Excel\XLSTART\PERSONAL.XLSB"
exit 0
The problem is that if i run the batch file once, keep excel open change the excelargs to CREO,wqhatever in batch file and rerun batch file the excelargs, dos not get updated!!!
So my theory is that excel either caches out the environment variable or that if it is being used by one instance the batch script can not set it
link with some info about passing arguments to excel:
https://superuser.com/questions/640359/bat-file-to-open-excel-with-parameters-spaces
Usually excel sees if there is a previous instance running and let this instance handle the file opening.
Is this important? Yes, in your case both requests to open the file are handled by the same excel process.
How does it make a difference? Environment variables are not shared. Each process has it own environment block that is initialized when the process is created (can be a customized block or a copy of the environment of the parent process) and once the environment is created for a process, only this process can change its environment.
In your case, when you start excel the new process gets a copy of the environment block of the cmd process. Later, when you change the cmd environment, the already running excel instance sees no changes in environment and, as the new request to open excel is converted to a request to the previous process, there is not a new process with a new copy of the cmd environment with the changes.
The only way I see to make it work is to force excel to start a new process (that will inherit the changes in the cmd instance) instead of reusing the previous one.
But this depends on the excel version. As far as I know, the 2013 version includes an /x switch to force separate process usage. For previous versions, maybe this question, or this one could help you.
Excel is open
Then i start the batch script:
The it does not open it as read only by default, but prompt me instead, not a big issue but a bit annoying, and it also make it impossible to loop through to run the batch several times for different input parameters.
A bit unsure how I should post this, couldnt paste images in comments, and to edit the the original question, which was how to start excel with enviroment variable in new instance (/x did the trick), but now /r does not work, Should I post as new question and refer to this one or can I leave it as an answer?

Call .reg File from Excel-VBA script

I want to call a .reg File out of my VBA script. I'm using Office/Excel 2013.
I know Excel can't run these files by itself, so i need to call the file via shell. The code i wrote doesn't work:
Sub deactivateHyperlinkWarnings()
Dim x
x = Shell("cmd /C C:\TEMP\DisableHyperlinkWarnings.reg")
End Sub
I found this piece of code somewhere on the web, but its not working. I don't even get an error message. The .reg File is located in C:\TEMP
What do i need to write to make it work?
Plus: Is it possible to suppress the MessageBoxes that are displayed when i run the .reg-File? When i start the file manually, i need to Hit "OK" like 3 Times. The people who are working with the Excelsheet later on shouldn't be seeing these things.
Instead of running cmd try to run reg. So in your case it should be x = Shell("reg import C:\TEMP\DisableHyperlinkWarnings.reg")
More info here

Excel error when using path name as parameter from command line

I am trying to launch a specific Excel document from the command line and pass in a variable in the form of a file path. On starting the Excel sheet runs a macro. This file path may contain spaces however when Excel interprets this it, I believe it tries to open up each section after a space as a new workbook. This results in a number of error warnings after the program runs as it obviously cannot file created from the substring.
The batch file looks like this
echo %~1
start excel.exe %USERPROFILE%\Desktop\Compare.xlsm /e/%1%
pause
EDIT: For Clarification. The batch file is activate when a file is drag and dropped onto it. The path of the file dropped is then stored in the %1 variable. If this file path has spaces then after each space Excel assumes that this is a new worksheet and tries to open it. The call to the Compare.xlsm which contains a macro that is going to use the path of the dragged file works correctly as it will always be on the desktop. My issue therefore is how to get Excel to take the entire path name stored in %1 and use it as one command line parameter rather than several calls to open new workbooks.
If the file that is used on the batch file does not contain any spaces then the errors do not occur. Is there any way of getting rid of the errors when using a file path that might have spaces e.g. C:\Users\My Documents\foobar.txt
Try this (TRIED AND TESTED)
echo %~1
start "excel.exe" "%USERPROFILE%\Desktop\Compare.xlsm" /e/%1%
pause
Notice the quotes around "Excel.Exe" and the file path?
Another example
echo %~1
start "excel.exe" "%USERPROFILE%\Desktop\Blah Blah.xlsm" /e/%1%
pause
I am assuming that you are running the code from a .Bat file
as Sid says, any paths that contain spaces must be quoted.
so to use a pth such as C:\My Documents\fubar.txt you would have this:
"C:\My Documents\fubar.txt"
and in your example:
echo
%~1
start excel.exe "%USERPROFILE%\Desktop\Compare.xlsm" /e/%1%
pause
EDIT:
When using a variable as the path, you need to include the quotes in the variable!

Directing the output of a cmd line procedure to Excel

I am running a cmd command on VBA using call Shell(), but I would like to get the output of the command line to paste it to a range.
something basic like:
forfiles /P C:\Directory\ /S /D %DATE:~4,10%
this returns 99% of the time either nothing or a single string (1 file name) for me which shows the files updated today.
I am hoping to get this output string pasted into a Range. Anyone know a way to redirect outputs?
I guess I can make the cmd line write to a csv and then import but that sounds inefficient to me.
Could you redirect the output of your batch script to a file, then read those file contents into a variable inside your VBA program? Then you could just load that variable value to your range.
You could read the file contents like this:
Open "C:\batch_output.txt" for input as #1
Input #1, textValue
Close #1
Your VBA code could also delete the batch output file when finished if needed.