0

this is my code, I have seen others closing excel sheets this way but why does this not work. There are no errors in the code execution but the app still seems to be running in the background

Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook Sheet = Excel.Workbooks.Open("C:\\Users\\Maxine\\Testing.xlsx");
            Microsoft.Office.Interop.Excel.Worksheet x = ((Microsoft.Office.Interop.Excel.Worksheet)Excel.ActiveSheet);
            Sheet.Close(false,Type.Missing,Type.Missing);
            Excel.Quit();
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
Max Price
  • 15
  • 4
  • what is the error you are getting? – NicoRiff Sep 12 '17 at 14:57
  • nothing, just executes and leaves the app running in the background – Max Price Sep 12 '17 at 14:58
  • Possible duplicate of [How do I properly clean up Excel interop objects?](https://stackoverflow.com/questions/158706/how-do-i-properly-clean-up-excel-interop-objects) – I.B Sep 12 '17 at 15:10
  • Possible duplicate of [C# - Proper way to open and close an excel file programmatically](https://stackoverflow.com/questions/30792531/c-sharp-proper-way-to-open-and-close-an-excel-file-programmatically) – Rahul Hendawe Sep 12 '17 at 15:12

1 Answers1

0

You need to actually release the COM object. See here, but you need to do Marshal.FinalReleaseComObject on the Excel object.

  • I tried using it and the result of the call is 0 but the program is still running in the background – Max Price Sep 12 '17 at 15:05
  • Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook Sheet = Excel.Workbooks.Open("C:\\Users\\JustinNew\\Desktop\\Testing.xlsx"); Microsoft.Office.Interop.Excel.Worksheet x = ((Microsoft.Office.Interop.Excel.Worksheet)Excel.ActiveSheet); Sheet.Close(false,Type.Missing,Type.Missing); Excel.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Excel); Am i using this right? – Max Price Sep 12 '17 at 15:13
  • 1
    @MaxPrice You need to release your WorkSheet, your Workbook and your Excel Application check the question I marked as duplicate. – I.B Sep 12 '17 at 15:17