Is there a dos command to close an excel worksheet ?

  • Thread starter Thread starter Emory Richter
  • Start date Start date
E

Emory Richter

In XP,
Is there a dos command to close an excel worksheet
not the excel application ?

I can find nothing on google.

I have tried the utility "pv"

C:\Windows\Command\pv -k -f file.xls

but that will close only processes
and file.xls is listed in Task Manager applications.

Thank you,
Emory
 
In XP,
Is there a dos command to close an excel worksheet
not the excel application ?

This one-liner worked for me. Use single percent signs if run from
the command line. As is it is written for batch. This uses only
tools built into XP.

for /f "tokens=2" %%a in ('tasklist /v ^|findstr /i /i /C:"file.xls"')
do taskkill /PID %%a

Cheers,
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
This one-liner worked for me.
for /f "tokens=2" %%a in ('tasklist /v ^|findstr /i /i /C:"file.xls"')
do taskkill /PID %%a

Clay, thanks for your response.
But, I am a novice
and the method of using this
seems beyond me.

Emory
 
Clay, thanks for your response.
But, I am a novice
and the method of using this
seems beyond me.

The only thing you really need to change is the file name.

Actually, I was writing up how this works when I saw a simpler way to
do this.

taskkill /FI "WindowTitle eq Microsoft Excel - file.xls"

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
Actually, I was writing up how this works when I saw a simpler way to
do this.

taskkill /FI "WindowTitle eq Microsoft Excel - file.xls"

Clay,
in XP with SP2
with other worksheets open and also WatchLists.xls open
when I try:
taskkill /FI "WindowTitle eq Microsoft Excel - WatchLists.xls"
I get:
ERROR: Invalid Argument/Option - 'WatchLists.xls'.

The title is right according to the Excel title bar.
As other worksheets are open
the Task Manager shows the title as only 'WatchLists.xls'.
but when I try:
taskkill /FI "WindowTitle eq WatchLists.xls"
I get:
INFO: No tasks running with the specified criteria.

I have also tried other variations eg:
taskkill /F /FI "WindowTitle eq Microsoft Excel - WatchLists.xls"
etc.
but always the error message appears.

Thank you for your past help and - any suggestions ?

Emory
 
Clay,
in XP with SP2
with other worksheets open and also WatchLists.xls open
when I try:
taskkill /FI "WindowTitle eq Microsoft Excel - WatchLists.xls"
I get:
ERROR: Invalid Argument/Option - 'WatchLists.xls'.

The title is right according to the Excel title bar.
As other worksheets are open
the Task Manager shows the title as only 'WatchLists.xls'.
but when I try:
taskkill /FI "WindowTitle eq WatchLists.xls"
I get:
INFO: No tasks running with the specified criteria.

I have also tried other variations eg:
taskkill /F /FI "WindowTitle eq Microsoft Excel - WatchLists.xls"
etc.
but always the error message appears.

Thank you for your past help and - any suggestions ?

Interesting. I was able to replicate your scenario. It looks like
only one instance of Excel is running, even though two Excel items are
on the TaskBark. I just noticed that even though there are two items
on the taskbar, there is really only one Excel window open.

This VBscript worked for me, YMMV.

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Microsoft Excel - Katherine's Family tree.xls"
WScript.Sleep 1000
WshShell.SendKeys "^w"
WScript.Sleep 2500

You can test the first timeout value, but I'd leave the bottom one
alone.

VBscript can only send keys to the active window. I believe
higher-end languages can send to windows if they know the 'handle'.

HTH
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
This VBscript worked for me, YMMV.

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Microsoft Excel - Katherine's Family tree.xls"
WScript.Sleep 1000
WshShell.SendKeys "^w"
WScript.Sleep 2500


It worked as a .vbs file for me also.
Clay, thanks for your effort on this question.
Emory
 
It worked as a .vbs file for me also.
Clay, thanks for your effort on this question.
Emory

Great, and I got a little education in the process. ; )

Cheers
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
Back
Top