Santiago Cuartas Rmrz
Santiago Cuartas Rmrz

Reputation: 171

Bat file that detect's when close an specific Excel workbook

I have this code that detects when the Excel.exe task is terminated:

:Excel
tasklist /nh /fi "imagename eq Excel.exe" | find /i "Excel.exe" >nul && (
goto Excel
) || (
goto Continue
)

:Continue

I want to modify this code to make it detect when I close an Excel workbook called

Database.xlsm

This is beacause I need to close ALL the Excel workbooks that I have open in the moment I execute the batch to make it go to

:Continue

I want it go to :Continue only when the Database.xlsm document is closed.

I hope you can help me. Thank you!

Upvotes: 1

Views: 979

Answers (2)

aschipfl
aschipfl

Reputation: 34929

You could try the following code, using findstr to filter for the window title of the process:

:Excel
timeout 1 /NOBREAK
tasklist /V /FI "IMAGENAME eq excel.exe" /FO LIST | findstr /RIXC:"Window Title: *Microsoft Excel - Database.xlsm" > nul && (
    goto :Excel
) || (
    goto :Continue
)
:Continue

You may need to adapt the partial string Microsoft Excel - Database.xlsm to your actual Excel window title.

I inserted timeout 1 /NOBREAK after line :Excel to avoid heavy CPU load.

Upvotes: 2

ShoeLace
ShoeLace

Reputation: 3576

try experiment with the windowtitle filter..

first use the /V flag to see teh window title.. then try filtering for it

ie

tasklist /v /FI "imagename eq Excel.exe" /FI "windowtitle eq Microsoft Excel - Database.xlsm" 

it may/may not work depending on exact excel and windows versions

or you can filter afterwards using your find

Upvotes: 0

Related Questions