|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
closing excel from c#Help please. The excel instance is removed from task manager when I
run the code below. If I uncomment the lines pertaining to a workbook, the instance of excel is not removed from task manager. can someone help? Thanks Excel.Application xl1 = new Excel.Application(); //Microsoft.Office.Interop.Excel.Workbook wb1 = xl1.Workbooks.Add(System.Reflection.Missing.Value); xl1.Visible = true; xl1.Quit(); //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1); //wb1 = null; xl1 = null; GC.Collect(); GC.WaitForPendingFinalizers(); You have to be more aware of the objects that you are using in Excel.
For example, you are creating the application, and you are releasing that. However, you are exposing the Workbooks property, which you have to release as well. Then you have to release the workbook that you have (which you are doing). Every property that returns an object, for the most part, you have to keep track of and release when you are done (through a call to ReleaseComObject). Then, when you call quit, and then ReleaseComObject on the application, Excel should close. Hope this helps. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com <pleaseexplaintom***@yahoo.com> wrote in message news:1176403017.337617.5110@p77g2000hsh.googlegroups.com... > Help please. The excel instance is removed from task manager when I > run the code below. If I uncomment the lines pertaining to a > workbook, the instance of excel is not removed from task manager. can > someone help? Thanks > > Excel.Application xl1 = new Excel.Application(); > //Microsoft.Office.Interop.Excel.Workbook wb1 = > xl1.Workbooks.Add(System.Reflection.Missing.Value); > xl1.Visible = true; > xl1.Quit(); > //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); > System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1); > //wb1 = null; > xl1 = null; > GC.Collect(); > GC.WaitForPendingFinalizers(); > > You have to be more aware of the objects that you are using in Excel. Thanks for the response....and I understand what u r saying....but how> For example, you are creating the application, and you are releasing that. > However, you are exposing the Workbooks property, which you have to release > as well. Then you have to release the workbook that you have (which you are > doing). > > Every property that returns an object, for the most part, you have to > keep track of and release when you are done (through a call to > ReleaseComObject). Then, when you call quit, and then ReleaseComObject on > the application, Excel should close. do I release the workbook object? System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); wb1 = null; doesn't do it. Thanks You also have to release the workbooks object (note the plural) that is
exposed by the workbooks property. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com <pleaseexplaintom***@yahoo.com> wrote in message news:1176412874.759075.311220@y80g2000hsf.googlegroups.com... >> You have to be more aware of the objects that you are using in Excel. >> For example, you are creating the application, and you are releasing >> that. >> However, you are exposing the Workbooks property, which you have to >> release >> as well. Then you have to release the workbook that you have (which you >> are >> doing). >> >> Every property that returns an object, for the most part, you have to >> keep track of and release when you are done (through a call to >> ReleaseComObject). Then, when you call quit, and then ReleaseComObject >> on >> the application, Excel should close. > > > Thanks for the response....and I understand what u r saying....but how > do I release the workbook object? > System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); > wb1 = null; > doesn't do it. > Thanks > <pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote news:1176403017.337617.5110@p77g2000hsh.googlegroups.com... You need to release *all* references to the Excel COM Interfaces (ReleaseComObject), force > Help please. The excel instance is removed from task manager when I > run the code below. If I uncomment the lines pertaining to a > workbook, the instance of excel is not removed from task manager. can > someone help? Thanks > > Excel.Application xl1 = new Excel.Application(); > //Microsoft.Office.Interop.Excel.Workbook wb1 = > xl1.Workbooks.Add(System.Reflection.Missing.Value); > xl1.Visible = true; > xl1.Quit(); > //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); > System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1); > //wb1 = null; > xl1 = null; > GC.Collect(); > GC.WaitForPendingFinalizers(); > a GC and wait for a finalizer run. Try following in a console application, it should work. using xl = Microsoft.Office.Interop.Excel; ... { static void Main() { xl.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); xl.Workbook wb = exApp.Workbooks.Add(System.Reflection.Missing.Value); xlApp.Visible = true; System.Threading.Thread.Sleep(1000); exApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); GC.Collect(); GC.WaitForPendingFinalizers(); // keep the client running, excell should be gone at this point System.Threading.Thread.Sleep(30000); } ... Willy. On Apr 13, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye***@telenet.be> wrote: Show quoteHide quote > <pleaseexplaintom***@yahoo.com> wrote in message Thanks for all your help. The problem was, as you said all along, I> > news:1176403017.337617.5110@p77g2000hsh.googlegroups.com... > > > > > > > Help please. The excel instance is removed from task manager when I > > run the code below. If I uncomment the lines pertaining to a > > workbook, the instance of excel is not removed from task manager. can > > someone help? Thanks > > > Excel.Application xl1 = new Excel.Application(); > > //Microsoft.Office.Interop.Excel.Workbook wb1 = > > xl1.Workbooks.Add(System.Reflection.Missing.Value); > > xl1.Visible = true; > > xl1.Quit(); > > //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); > > System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1); > > //wb1 = null; > > xl1 = null; > > GC.Collect(); > > GC.WaitForPendingFinalizers(); > > You need to release *all* references to the Excel COM Interfaces (ReleaseComObject), force > a GC and wait for a finalizer run. > Try following in a console application, it should work. > > using xl = Microsoft.Office.Interop.Excel; > .. > > { > static void Main() { > xl.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); > xl.Workbook wb = exApp.Workbooks.Add(System.Reflection.Missing.Value); > xlApp.Visible = true; > System.Threading.Thread.Sleep(1000); > exApp.Quit(); > System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); > System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); > GC.Collect(); > GC.WaitForPendingFinalizers(); > // keep the client running, excell should be gone at this point > System.Threading.Thread.Sleep(30000);} > > .. > > Willy.- Hide quoted text - > > - Show quoted text - had a reference I was not releasing. Now I do the following (try to close wb without saving), but the wb.close statement causes the app to stay in task manager as does the wb.SaveAs. Any suggestions? Thank you. wb.Close(Missing.Value, Missing.Value, Missing.Value); wb.SaveAs("c:\\test3.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Missing.Value); xl.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(range); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(xl); <pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote news:1176464814.767539.280260@n59g2000hsh.googlegroups.com... You have to close after you have saved the wb.> On Apr 13, 5:13 am, "Willy Denoyette [MVP]" > <willy.denoye***@telenet.be> wrote: >> <pleaseexplaintom***@yahoo.com> wrote in message >> >> news:1176403017.337617.5110@p77g2000hsh.googlegroups.com... >> >> >> >> >> >> > Help please. The excel instance is removed from task manager when I >> > run the code below. If I uncomment the lines pertaining to a >> > workbook, the instance of excel is not removed from task manager. can >> > someone help? Thanks >> >> > Excel.Application xl1 = new Excel.Application(); >> > //Microsoft.Office.Interop.Excel.Workbook wb1 = >> > xl1.Workbooks.Add(System.Reflection.Missing.Value); >> > xl1.Visible = true; >> > xl1.Quit(); >> > //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1); >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1); >> > //wb1 = null; >> > xl1 = null; >> > GC.Collect(); >> > GC.WaitForPendingFinalizers(); >> >> You need to release *all* references to the Excel COM Interfaces (ReleaseComObject), >> force >> a GC and wait for a finalizer run. >> Try following in a console application, it should work. >> >> using xl = Microsoft.Office.Interop.Excel; >> .. >> >> { >> static void Main() { >> xl.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); >> xl.Workbook wb = exApp.Workbooks.Add(System.Reflection.Missing.Value); >> xlApp.Visible = true; >> System.Threading.Thread.Sleep(1000); >> exApp.Quit(); >> System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); >> System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); >> GC.Collect(); >> GC.WaitForPendingFinalizers(); >> // keep the client running, excell should be gone at this point >> System.Threading.Thread.Sleep(30000);} >> >> .. >> >> Willy.- Hide quoted text - >> >> - Show quoted text - > > Thanks for all your help. The problem was, as you said all along, I > had a reference I was not releasing. > Now I do the following (try to close wb without saving), but the > wb.close statement causes the app to stay in task manager as does the > wb.SaveAs. Any suggestions? Thank you. > > wb.Close(Missing.Value, Missing.Value, Missing.Value); > wb.SaveAs("c:\\test3.xls", > Type.Missing, Type.Missing, Type.Missing, > Type.Missing, Type.Missing, > Excel.XlSaveAsAccessMode.xlNoChange, > Type.Missing, Type.Missing, Type.Missing, > Type.Missing, Missing.Value); > > xl.Quit(); > > System.Runtime.InteropServices.Marshal.ReleaseComObject(range); > > System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); > > System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); > > System.Runtime.InteropServices.Marshal.ReleaseComObject(xl); > Also you need to look at the object model for Excel and Office before you start coding in the wild. And, you need to add some error handling to your code, errors like above should throw an Exception, don't know why you didn't mention this, you aren't running this from a service like IIS do you? Willy.
Other interesting topics
How to capture key
Generics Interaction of windows service with UI Modify old style .NET 1.1 object collections to support LINQ query NotifyIcon (System Tray) VB6 .NET Interop - Passing Array of Objects or Object Collection Can someone show an example of how to read an XML file into an XMLDocument object? Need Help: Common IsolatedStorage per user regardless of application How to use object? GridView not being displayed: Lil silly mistake i guess.... |
|||||||||||||||||||||||