I want to open an Excel doc from my .NET 2.0 WinForm application.
I have a list of UNC path names of Excel documents a user might want
to open.
The way I'm doing it now works, but when the user closes Excel, it is
still running; I can see it in the list of processes in TaskManager.
My WinForm app open the document this way:
Dim pathname As String = ListBox1.SelectedItem.ToString()
Dim excelApp As Excel.Application
excelApp = New Excel.Application()
excelApp.Workbooks.Open(pathname)
excelApp.Visible = True
excelApp = Nothing
Even though I am setting excelApp to nothing, my WinForm app is
holding on to Excel after the user closes it.
Is there a better approach to opening Excel so that it will close when
the user closes the Excel window?
Mike - 25 Jun 2007 23:24 GMT
Have you tried
myWorkBook.Close(false,Type.Missing,Type.Missing);
Marshal.ReleaseComObject(myWorkBook);
> I want to open an Excel doc from my .NET 2.0 WinForm application.
>
[quoted text clipped - 18 lines]
> Is there a better approach to opening Excel so that it will close when
> the user closes the Excel window?
Robbe Morris - MVP C# - 26 Jun 2007 03:16 GMT
You have got to set literally every object reference
including the sheet, the workbook, and the application
object to null in your finally clause.
The sample in this article will show you:
http://www.eggheadcafe.com/articles/20021012.asp

Signature
Robbe Morris
EggHeadCafe.com
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
>I want to open an Excel doc from my .NET 2.0 WinForm application.
>
[quoted text clipped - 18 lines]
> Is there a better approach to opening Excel so that it will close when
> the user closes the Excel window?
BrenB - 26 Jun 2007 18:16 GMT
Thank you both, Robbe and Mike, for responding to my post. Your
suggestions worked for me.
I nulled all my objects after calling the ReleaseComObjects method on
them.
I did not close the workbooks or quit the app in my code. When I
closed Excel using its user interface, I saw its process end in Task
Manager.
I appreciate the help.
Bren