Click here to Skip to main content
14,769,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

In my application i am using a third party tool to show an uploaded excel sheet and to edit and save it. During this process i need to delete the existing excel sheet and edit and save new excel sheet. I have tried to delete but the excel will not delete and throw error as excel sheet in use by another process cannot delete. So i decided to close that excel sheet or kill the process so i wrote code as below:
using Excel = Microsoft.Office.Interop.Excel;

Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;


    _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    WB = _Excel.Workbooks.Open(@"FIENAME",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

    WB.Close(false , Type.Missing, Type.Missing);

    //do something

catch (Exception ex)
    WB.Close(false, Type.Missing, Type.Missing);





but this will still throw same error that the excel sheet is being used by another process... Can u please help me how can we close that excel or workbook programatically. Or what programatic solution this can have.. as killing process might effect other projects in Server...

Please help!! Thanking You in advance!!
ZurdoDev 14-Aug-13 7:26am
You'll need to find a way to get the 3rd party tool to close it.
Maciej Los 14-Aug-13 7:36am
ZurdoDev 14-Aug-13 7:42am
Because OP said he got an error about it being in use when he tried to close it.
Maciej Los 14-Aug-13 7:55am
Have you seen my comment to the OP's question?
ZurdoDev 14-Aug-13 8:03am
Yes, isn't your code the same as the OPs? What am I missing? Of course you comment it out in the catch but otherwise what is different?
Maciej Los 14-Aug-13 7:36am
Isn't a typo:
WB.Close(false , Type.Missing, Type.Missing); //->first time
catch (Exception ex)
//WB.Close(false, Type.Missing, Type.Missing); //->second time
//throw; //???
//here you should show a message error
Shruthi.BT 14-Aug-13 7:54am
Actually i am just trying to open an excel and close just to check if that works.. then code to delete i have written.. but when delete line is exccecuted it throws that excel is used by another process cannot delete.. the other process is that the file is being viewed or opened using third party tool .. so i need to kill that excel process i guess... but how can i do that progrmatically?? and also if i kill process will it effect other projects??
idenizeni 14-Aug-13 12:19pm
Is it possible you created the situation in testing? If during your testing you run the process multiple times and Excel doesn't completely close you may have some 'phantom' excel processes running, and attached to the file. Open Task Manager and end any Excel processes that are running. Now try your delete test. If the issues was caused by the 'phantom' processes then you should have different results this time.

I've experienced this with Word. To solve it I wrapped the Word object in a Singleton Class to ensure it's properly disposed and to prevent multiple Word references.
Shruthi.BT 17-Aug-13 3:45am
Actually this is not opening Excel manually n closing... i am opening the excel with a third party tool so the excel is viewed or accesed by third party tool... so if i try to delte the excel maually also it is not possible n also the process i cannot manually end in Server.. if i try to delete the excel maually it shows w3wp.exe some error... how can i stop that and if i stop that process will that effect my application or any other projects in Server..???

1 solution

First of all, please, read my comment to the question, then have a look here: Closing excel workbook from C#[^]
Shruthi.BT 16-Aug-13 6:59am
Hey Hi.... this works in my loocal system but in Server it does not.. that is we use Windows Server 2008 and it is 64 bit.. but the Server does not have MS Office so it does not support the Interop that is the namespace...


How can i solve this... any other way i can close the process?? when i gave response.write it showed this error...

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154. at GetFiles.StartExcel()

how can we solve this....?? ANY IDEA ... plz Help!1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900