BasiliskHill
BasiliskHill

Reputation: 56

How to check if an Excel spreadsheet is running, use it in an if command and run the sheet if it isn't already running

I have been trying to set something up using batch. What I have been trying to set up is something that would open an excel spreadsheet containing some of the homework I had written up that I needed to get done. Though, I had run into a small problem when I had tried to check if the spreadsheet was open. I tried looking at a few solutions to my problem but I never really understood how to use them or they weren't made for xlxs. What I currently have is this code:

@echo off
SET "stime=16:00:00.00"
SET "etime=24:00:00.00"
:start
IF %time% GEQ %stime% (
    IF %time% LEQ %etime% (
        START Homework.xlsx
        ECHO Hey
        GOTO start
    )
) ELSE (
    GOTO start
    )
IF %time% LEQ %etime% (
    IF %time% GEQ %stime% (
        START Homework.xlsx
        ECHO Hey
        GOTO start
    )
) ELSE (
    GOTO start
    )

Upvotes: 1

Views: 2573

Answers (1)

Regie Baguio
Regie Baguio

Reputation: 241

The thing is your code is looping like a fork bomb, I guess you're trying to stop the loop by verifying if an instance of Excel is running.

tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" //commands here

It will check if any excel file open, running, or even previewed in preview pane.

Upvotes: 3

Related Questions