3

i have 4 excel files (city.xls, rule.xls,adv.xls and maping.xls), where all of excel files being opened. how to force close only one file (Ex: maping.xls) with taskkill in batch/CMD programing?

flyingbird013
  • 446
  • 2
  • 12
  • 28

2 Answers2

3

Taskkill will eliminate a process with all its windows. Your workbooks can be all open under the same instance of excel, so, taskkill can not do what you need.

You can use some tools like AutoIt, nircmd, cmdow, ... to just close the window that you need, but you will have to deal with the posibility of dialog asking to save the workbook.

Here is a batchfile (just save as .cmd and change the xlsx file) wich includes a script part to search the workbook and close it without dialogs. No bulleproof, and only works under the same session of the user that opened the workbook, but just a starting sample.

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************

    rem call javascript part of batch file
    cscript //nologo //e:Javascript "%~dpnx0" /workbook:"j:\wherever\myWorkbook.xlsx"

    rem End of batch area. Ensure batch ends execution before reaching
    rem javascript zone
    exit /b

@end
// **** Javascript zone *****************************************************

    if (!WScript.Arguments.Named.Exists('workbook')) {
        // if no workbook argument, no work to do
        WScript.Quit(1);
    };

    // retrieve workbook name
    var workbook = WScript.Arguments.Named.Item('workbook');

    var exitCode = 0;
    try {
        // search for workbook application
        var wb = GetObject(workbook);

        // get handle to application containing workbook
        var app = wb.Application;

        // close workbook. Ask for save = false
        wb.Close( false );

        // if no more workbooks open in application, close it
        if (app.Workbooks.Count == 0){ 
            app.Quit();
        };

    } catch(e){
        // some kind of problem with objects
        // WScript.Echo( e.description );
        exitCode = 2;
    };
    app =null;
    wb  =null;
    WScript.Quit(exitCode);
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 1
    i think each workbook is in separate process , but I like this script.+1 – npocmaka Oct 29 '13 at 12:49
  • Just tested with excel 2007, don't know other versions. If you double click a excel file, a process is started. If you double click in another excel file, it is opened in a new window under the same process. Now, if you directly call excel from run command or from windows menu, a new process will be created. So, it depends of the user and how opens the workbooks. – MC ND Oct 29 '13 at 12:57
1

From Taskkill's Help (taskkill /?)

TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"

David Candy
  • 735
  • 5
  • 8
  • 1
    If all workbooks are opened in same instance of the executable, all workbooks will be closed, as executable is closed – MC ND Oct 29 '13 at 07:48
  • `"PID ge 1000"` id probably not needed here.And probably for window title `eq` should be used.+1 any way. – npocmaka Oct 29 '13 at 07:49