How to return information from Excel to the caller - excel

I start Excel from the command line and my add-on does some work. when it's done, I want to return some info to the caller. At least 0/1 for success failure or better also an optional error-message.
By caller I mean the command or process that started Excel. e.g. in a Windows command script I could call excel like this:
Excel.exe SomeWorkbook.xlsx /p C:\Somedir /e
when you call an executable in Windows, it can return an numeric code or set an error.
in a script you could check the result like this:
if %errorlevel% neq 0 (
echo some error occurred...
)
MessageBoxes, etc. are no option, because this whole task should be triggered by another application automatically without any user-interaction.
How can we do that?

You could use the status bar:
Application.StatusBar = “your message here”
As far as I know, the message box requires a button to be clicked: macro will wait...

I ended up using text files: i.e when the add-on finished correctly, it will create an empty file OK.txt and when an error occurred it will create a file named ERR.txt that contains the error-message.
Now it's easy for the calling script to check the result:
OK.txt exists: everything is fine - delete OK.txt
no file exists: a fatal error has happened: show a general error message
ERR.txt exists: an error occured: maybe display the error text (contents of the text-file) to the user, delete ERR.txt

Related

Retrieve underlying file of tee command

References
Fullcode of what will be discussed here:
https://github.com/djon2003/com.cyberinternauts.linux.backup
activateLogs question that solved how to log to file and screen: https://stackoverflow.com/a/70792273/214898
Limitation
Just a small reminder from the last question: this script is executed on limited environment, on a QNAP (NAS).
Background
I have a function that activate logging which now has three modes: SCREEN, DISK, BOTH. With some help (from the question of the link above), I achieve to make work the BOTH option. DISK & BOTH use a file descriptor numbered 3. The first is pointing to a file and the second to stdout.
On exit of my script (using trap), it detects if there were logged errors and send them via email.
Code
function sendErrorMailOnExit()
{
## If errors happened, then send email
local isFileDescriptor3Exist=$(command 2>/dev/null >&3 && echo "Y")
if [ "$isFileDescriptor3Exist" = "Y" ]; then
local logFile=$(readlink /proc/self/fd/3 | sed s/.log$/.err/)
local logFileSize=$(stat -c %s "$logFile")
if [ $logFileSize -gt 0 ]; then
addLog "N" "Sending error email"
local logFileName=$(basename "$logFile")
local logFileContent=$(cat "$logFile")
sendMail "Y" "QNAP - Backup error" "Error happened on backup. See log file $logFileName\n\nLog error file content:\n$logFileContent"
fi
fi
}
trap sendErrorMailOnExit EXIT
Problem
As you can see, this works well because the file descriptor #3 is using a file. But now, using the BOTH option, the file descriptor #3 is pointing to stdout and the file is written via tee. Hence my question, how could I get the location of the file of tee.
Why not only using a variable coming from my function activateLogs would you say? Because, this function relaunches the script to be able to get all the logs not caught before the function is called. Thus why using this method to retrieve the error file location.
Possible solutions, but not the best (I hope)
One way would be to pass the file location through a script
parameter, but I would prefer not do that if that can be avoid.
Another, would be to create a "fake" file descriptor #4 (probably my best solution up to now) that would always point to the file.
Does anyone have an idea?
I finally opted for the creation of a "fake" file descriptor #4 that does not nothing except pointing to the current log file.

Use python, to open cmd window with the directory supplied as an argument as the current directory

I want to use Python to open a directory in a Win10 cmd window, and keep the window open.
I made a batch file named: open_dir_in_cmd_window.CMD:
CD /D %1
I tested that batch file successfully, by creating another batch file named, Test.cmd:
Rem "open_dir_in_cmd_window.CMD" "f:\backup"
"open_dir_in_cmd_window.CMD" "f:\backup"
A very helpful webpage provides the following example, which I seem unable to follow correctly:
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
I made a python script, which contains the following lines, which alas, triggers an error message:
import subprocess
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
When I open a Command Prompt window and run:
"C:\ProgramData\Anaconda3\python.exe" E:\open_dir.py
I get an error message, SyntaxError: invalid syntax, with this:
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
^
I've tried many different permutations of double quoting and can't figure out the right way to do it.
I have spent many hours hunting on the web and trying to figure this out and I do not know what to do.
Any suggestions would be appreciated.

task scheduler is displaying an error "The specified query is invalid"

I am running Windows 10.
I'm trying to run a backup job (C:\WINDOWS\system32\robocopy.exe) using Task Scheduler. But I was getting issues. To figure out what was going on, I turned on the history log. Unfortunately since doing that, every time I click the History tab I get the following error:
Dialog Title: "Query Error"
Dialog Message: "One or more logs in the query have errors."
The table in the dialog: "Microsoft-Windows-Task-Scheduler/Operational | The specified query is invalid"
"The events displayed are partial results."
Nothing appears in the History pane, so I cannot debug either problem. Does anyone know what is going on?
I am having the exact same error as in
https://www.experts-exchange.com/questions/27676176/Query-Error-In-Microsoft-Task-Scheduler.html
The answer is at
http://www.minasi.com/forum/topic.asp?TOPIC_ID=27906
The answer site (www.minasi.com) is apparently moved and did not retain the answer so cannot try the fix that solved his problem.
Please check if you have single quotes (apostrophes) in the task name. It is known that the apostrophe in the task name breaks task history. To remove them you can try following batch file (it exports task to the xml file, imports it with the new name and removes the old task):
#set "tn=my PC's task"
#if not "%tn%"=="%tn:'=%" echo renaming "%tn%" ==^> "%tn:'=%" && ^
schtasks.exe /query /tn "%tn%" /xml>"%temp%\%tn:\=_%.xml" && ^
schtasks.exe /create /tn "%tn:'=%" /xml "%temp%\%tn:\=_%.xml" && ^
schtasks.exe /delete /tn "%tn%" /f && del /q /f "%temp%\%tn:\=_%.xml"

Post build event in VS2012..Running a Batch file

Am trying to run a Batch file in Post build event in Visual studio.
Referred Can we execute a .bat file in post build event command line in visual studio? for reference.
When i post the line
xcopy "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin" /S in postbuild
am getting the expected result
Same line i put in bat and tried calling
call "$(SolutionDir)\Deploy.bat"
or
call "Physical path\deploy.bat"
Am getting excited with code 1. What am i doing wrong here ?
Can i specify macros inside batch file ?
Thanks
You are getting a VS error because it returned an exit code that is not 0. This does not necessarily mean there was an error.
The error code returned means that no files were copied.
These are the return codes for Xcopy:
Exit Code
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed Ctrl+C to terminate xcopy.
4 Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
5 Disk write error occurred.
Try this code in your batch file. Use the /Y so that you will not have to deal with any prompts. You can handle the return code of 1 with another action or just return 0.
VS Post Build command line code:
CALL "$(SolutionDir)"Deploy.bat "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin"
Deploy.bat file
Xcopy %1 %2 /S /Y
If errorlevel 1 #exit 0

cscript - Invalid procedure call or argument when running a vbs file

I've been trying to use check_time.vbs to check the Windows time.
Here's the script: http://pastebin.com/NfUrCAqU
The help message could be display:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs /?
check_time.vbs V1.01
Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]
Options:
serverlist (required): one or more server names, coma-separated
warn (required): warning offset in seconds, can be partial
crit (required): critical offset in seconds, can be partial
biggest (optional): if multiple servers, else use default least offset
Example:
cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest
But I get the following error when running:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs 0.asia.pool.ntp.org 20 50
C:\Program Files\NSClient++\scripts\check_time.vbs(53, 1) Microsoft VBScript run
time error: Invalid procedure call or argument
The screenshot:
Manually execute w32tm still works fine:
What might be the cause of this?
IIRC the (53,1) indicates that the error is on line 53. At this point it is expecting an array of regexp matches with at least one item (index 0) and one sub-match (i.e. the object in position 0 in the array has an array property called SubMatches with at least one item in it.
It is not checking to make sure this structure is present and correct before trying to use it.
My assumption is that the regexp call is failing to find anything to match, presumably because the input string is not in the expected format. You could output the content of strOutput before that line to see what it contains - it could be a date/time representation in a different localized form than the regexp is designed for. You could also output the content of input after each call to objProc.StdOut.ReadLine - this would show you if the call to w32tm.exe returned a useful error message that is being skipped over by the script which is just looking for the value returned when all is well and ignoring the possibility of different output.
The culprit is the /nowarn argument:
w32tm /monitor /nowarn /computers:0.asia.pool.ntp.org
The following arguments were unexpected: /nowarn
Exiting with error 0x80070057
Remove it from the script, and now it works:
cscript //NoLogo check_time.vbs 0.uk.pool.ntp.org 20 50
NTP OK: Offset -2.4262131 secs|'offset'=-2.4262131s;20;50;

Resources