Pregunta

I created a task in the Windows 7 schtasks tool. It reminds me on the 15th of the month, by opening an excel workbook that I should look at and update. Handy.

Problem is, there is about a 60% chance I already have Excel open at the time this pops up, and I suppose a small chance I even have that workbook open. Then the result is not at all elegant. The task tries to open another copy of Excel and this complains about read-only access, and likewise with the workbook itself.

Is there a way in SCHTASKS, or in a BATCH file (or any other handy Windows tool you'd recommend) to first check if Excel is open and then if it is, just switch to it, rather than open a second copy of Excel? I've seen a note about using TASKLIST in a .BAT file to check if it is open:

 TASKLIST /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | FIND /I /N "EXCEL.EXE">NUL
 IF "%ERRORLEVEL%" EQ "0" START excel "C:\Project4\MonthlyTracking.xlsx"

but I don't know how to react in the BATCH file if it is open. I don't see a way in a BATCH file to tell the already-running excel process to open a file.

Many thanks!

¿Fue útil?

Solución

Instead of trying to start Excel with the file, start just the file:

"C:\Project4\MonthlyTracking.xlsx"

This will open excel, if it is not running with that file, It will open the file within a running excel, and it will give you an error, if it is already open - which you can use:

"C:\Project4\MonthlyTracking.xlsx" 2>nul
if errorlevel neq 0 echo file already opened 

Otros consejos

You can let Windows ShellExecute feature logic to start a file using the default associated program (in this case Excel), how to do it? just trying to run the file directly instead passing the file as an argument of the application.

TASKLIST /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | FIND /I "EXCEL">NUL && (
    START /B "ShellExecute Excel File" "C:\Project4\MonthlyTracking.xlsx"
)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top