Is there a dos command to close an excel worksheet ?

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
 
C

Clay Calvert

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"
 
E

Emory Richter

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
 
C

Clay Calvert

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"
 
E

Emory Richter

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
 
C

Clay Calvert

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"
 
E

Emory Richter

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
 
C

Clay Calvert

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top