3

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!

Mark Goldfain
  • 731
  • 2
  • 8
  • 24

2 Answers2

3

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 
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    @MarkGoldfain: the "translation" between filetype and executable is done via `assoc` (what filetype is that file) and `ftype` (what executable is used for that filetype). Try `assoc .xlsx` (which gives me `Excel.Sheet.12`). Then type "ftype Excel.Sheet.12` (which gives me `"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" /dde`) – Stephan Oct 18 '13 at 12:40
  • 1
    @MarkGoldfain: I don't know a way to tell an application from Batch, what to do. Instead you can use `msg %username% "MonthlyTracking already open - please don't forget to update it"` (if %errorlevel% eqs 32...) – Stephan Oct 18 '13 at 12:45
1

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"
)
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Thanks. I think this is roughly the same answer as Stephan's as far as I understand them. You introduce ShellExecute, and I don't know much about that. It may offer some advantages, but his seems to work for what I wanted without that ... up to the final finesse question I had (see there). – Mark Goldfain Oct 17 '13 at 21:46