Home All Groups Group Topic Archive Search About
Author
12 Apr 2007 6:36 PM
pleaseexplaintome_2
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();

Author
12 Apr 2007 7:35 PM
Nicholas Paldino [.NET/C# MVP]
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.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

<pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote
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();
>
Are all your drivers up to date? click for free checkup

Author
12 Apr 2007 9:21 PM
pleaseexplaintome_2
>     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
Author
13 Apr 2007 5:06 AM
Nicholas Paldino [.NET/C# MVP]
You also have to release the workbooks object (note the plural) that is
exposed by the workbooks property.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

<pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote
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
>
Author
13 Apr 2007 9:13 AM
Willy Denoyette [MVP]
<pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote
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.
Author
13 Apr 2007 11:46 AM
pleaseexplaintome_2
On Apr 13, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye***@telenet.be> wrote:
Show quoteHide quote
> <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);
Author
13 Apr 2007 1:45 PM
Willy Denoyette [MVP]
<pleaseexplaintom***@yahoo.com> wrote in message
Show quoteHide quote
news:1176464814.767539.280260@n59g2000hsh.googlegroups.com...
> 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);
>



You have to close after you have saved the wb.
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.

Bookmark and Share