cancel
Showing results for 
Search instead for 
Did you mean: 

Closing all Excel workbooks

lucie16
Active Participant
0 Kudos

Hi all,

I have an automation, where I create and open some number of Excel workbooks. I don't know, how many there will be. It depends on specific parameters. But I know the maximum number of them.

I want to close all opened workbooks at the end of automation. But I don't know how. Any ideas?

Or if there is a tool that tells me whether a specific workbook is opened - that would be also helpful.

Or is there an option to close specific Excel?

P.S. I use Cloud Studio and attended mode.

Regards

Lucie

View Entire Topic
janv7306
Active Participant
0 Kudos

Dear Lucie,

The usual solution is to keep the Excel file open only while working with it. But in my case I am not sure if there is some file open before I start working with excel in iRPA. Below I am attaching the script I use to close all Excel instances. Credit belongs to Mohamed:

https://blogs.sap.com/2021/04/11/sap-irpa-v2-how-to-use-vb-script-to-manipulate-excel-on-cloud-studi...

function formatEscapCaracters(f) {
    return f.toString().
        replace(/^[^\/]+\/\*!?/, '').
        replace(/\*\/[^\/]+$/, '');
}

irpa_core.core.log('custom script close any opened Excel', irpa_core.enums.logType.Info, 'excelAutomation');

try {
    var MSScrCtrl = irpa_core.activeX.create("MSScriptControl.ScriptControl");
    MSScrCtrl.AllowUI = 1;
    MSScrCtrl.Language = 'VBScript';

    var VBScode = formatEscapCaracters(function () {/*!
        Sub closeAnyOpenedExcel()
            For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = 'EXCEL.EXE'")
                Process.Terminate
            Next
        End Sub
    */});

    MSScrCtrl.AddCode(VBScode);
    MSScrCtrl.Run("closeAnyOpenedExcel");

    irpa_core.core.log('custom script close any opened Excel', irpa_core.enums.logType.Info, 'excelAutomation');

} catch (error) {

    irpa_core.core.log('closeAnyOpenedExcel Exception occured:' + error, irpa_core.enums.logType.Error, 'excelAutomation');

}
lucie16
Active Participant
0 Kudos

Thank you, this code is what I need.