Close an excel app in C#

  • Thread starter Thread starter Richie
  • Start date Start date
R

Richie

I want to close excel application from C#, this is the piece of code I
am trying
"System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

Also tried excelApp.Quit();

This is fine, but on viewing "Task List" the application tab shows
"Excel.exe" running. And if I run the program again the message
"Administrator is using the file, file will open in readonly mode" shows.

Any ideas ?
 
Hi Patrice,
Thanks for your time.
I am really new to .net, can you tell me explicitly how to do it through
code ?
Thanks again !
 
Nothing to do with .NET.

I meant I suspect the problem could arise when you try to close the Excel
application without first closing explicitely the workbook file.
I would try to close the workbook using the Excel object model before
quitting using the Excel object.

At least you shouldn't have any more the "file is open" error. You'll have
still to check if Excel is still in memory or not...

For now I believe that Excel doesn't "closes" properly if you don't close
yourself the workbook you are working on (remember in interactive mode
you'll have a dialog box asking to save or not).


Patrice

--
 
"System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

following code works for us, however it is written in VB.NET to simpify the
automation. thus, you will have to rewrite it using reflection to user it
under C#.

the trick is to close all open workbooks before you call quit on Excell
object.

Wiktor Zychla

If Not (oExcel Is Nothing) Then

For i = 1 To oExcel.workbooks.count

oExcel.workbooks(i).close(False)

Next

oExcel.quit()

End If

oExcel = Nothing

GC.Collect()

GC.WaitForPendingFinalizers()
 
Back
Top