I have a vb.net app that opens an excel worksheet, reads data and then closes
the sheet. Im noticing that the Excel process is still running after I have
closed and disposed of my excel objects.

The following code (Test1) demonstrates the essence of what I am doing.
When I check the processes while ruinning the method, I notice that the Excel
process remains after exiting the sub (and until I exit the application)

Sub Test1
Dim objExcelApp As New Excel.Application

Dim objExcelWorkBook As Excel.Workbook =
objExcelApp.Workbooks.Open("C:\Test.xls")
Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)

Dim objRange As Excel.Range
objRange = objExcelWorksheet.Range("A1")
MsgBox(objRange.Text)
''http://www.vbforums.com/archive/index.php/t-396405.html
objRange = Nothing
objExcelWorksheet = Nothing
objExcelWorkBook.Close()
objExcelWorkBook = Nothing
objExcelApp.Workbooks.Close()
objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)

objExcelApp = Nothing

End Sub

When I strip the code dow to this (Test2) I notice that the process is
created on line
Dim objExcelApp As New Excel.Application
and killed on line
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)

Sub Test2
Dim objExcelApp As New Excel.Application

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)

objExcelApp = Nothing
End Sub


What is it that is keeping a reference to the Excel process and how do I
kill the process?

Re: Excel does not die using automation by Scott

Scott
Sun Jul 06 09:23:52 CDT 2008

You must call ReleaseComObject(obj) on EACH COM object you've created.

That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
objects one might instantiate within the Excel.Application.

"Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
>I have a vb.net app that opens an excel worksheet, reads data and then
>closes
> the sheet. Im noticing that the Excel process is still running after I
> have
> closed and disposed of my excel objects.
>
> The following code (Test1) demonstrates the essence of what I am doing.
> When I check the processes while ruinning the method, I notice that the
> Excel
> process remains after exiting the sub (and until I exit the application)
>
> Sub Test1
> Dim objExcelApp As New Excel.Application
>
> Dim objExcelWorkBook As Excel.Workbook =
> objExcelApp.Workbooks.Open("C:\Test.xls")
> Dim objExcelWorksheet As Excel.Worksheet =
> objExcelWorkBook.Sheets(1)
>
> Dim objRange As Excel.Range
> objRange = objExcelWorksheet.Range("A1")
> MsgBox(objRange.Text)
> ''http://www.vbforums.com/archive/index.php/t-396405.html
> objRange = Nothing
> objExcelWorksheet = Nothing
> objExcelWorkBook.Close()
> objExcelWorkBook = Nothing
> objExcelApp.Workbooks.Close()
> objExcelApp.Quit()
>
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> objExcelApp = Nothing
>
> End Sub
>
> When I strip the code dow to this (Test2) I notice that the process is
> created on line
> Dim objExcelApp As New Excel.Application
> and killed on line
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> Sub Test2
> Dim objExcelApp As New Excel.Application
>
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> objExcelApp = Nothing
> End Sub
>
>
> What is it that is keeping a reference to the Excel process and how do I
> kill the process?



Re: Excel does not die using automation by MSDNNospam248

MSDNNospam248
Sun Jul 06 17:52:28 CDT 2008

Using the following as test code, the Excel process remains. Can you see a
reason for this?

Sub Test
Dim objExcelApp As New Excel.Application
Dim objExcelWorkBook As Excel.Workbook =
objExcelApp.Workbooks.Open("C:\Test.xls")
Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
Dim objRange As Excel.Range

objRange = objExcelWorksheet.Range("A1")
MsgBox(objRange.Text)

objExcelWorkBook.Close()
objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)

objRange = Nothing
objExcelWorksheet = Nothing
objExcelWorkBook = Nothing
objExcelApp = Nothing
End Sub
"Scott M." wrote:

> You must call ReleaseComObject(obj) on EACH COM object you've created.
>
> That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
> objects one might instantiate within the Excel.Application.
>
> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
> >I have a vb.net app that opens an excel worksheet, reads data and then
> >closes
> > the sheet. Im noticing that the Excel process is still running after I
> > have
> > closed and disposed of my excel objects.
> >
> > The following code (Test1) demonstrates the essence of what I am doing.
> > When I check the processes while ruinning the method, I notice that the
> > Excel
> > process remains after exiting the sub (and until I exit the application)
> >
> > Sub Test1
> > Dim objExcelApp As New Excel.Application
> >
> > Dim objExcelWorkBook As Excel.Workbook =
> > objExcelApp.Workbooks.Open("C:\Test.xls")
> > Dim objExcelWorksheet As Excel.Worksheet =
> > objExcelWorkBook.Sheets(1)
> >
> > Dim objRange As Excel.Range
> > objRange = objExcelWorksheet.Range("A1")
> > MsgBox(objRange.Text)
> > ''http://www.vbforums.com/archive/index.php/t-396405.html
> > objRange = Nothing
> > objExcelWorksheet = Nothing
> > objExcelWorkBook.Close()
> > objExcelWorkBook = Nothing
> > objExcelApp.Workbooks.Close()
> > objExcelApp.Quit()
> >
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >
> > objExcelApp = Nothing
> >
> > End Sub
> >
> > When I strip the code dow to this (Test2) I notice that the process is
> > created on line
> > Dim objExcelApp As New Excel.Application
> > and killed on line
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >
> > Sub Test2
> > Dim objExcelApp As New Excel.Application
> >
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >
> > objExcelApp = Nothing
> > End Sub
> >
> >
> > What is it that is keeping a reference to the Excel process and how do I
> > kill the process?
>
>
>

Re: Excel does not die using automation by Scott

Scott
Sun Jul 06 19:14:13 CDT 2008

Are you checking to see if Excel is running when you are debugging your code
in Visual Studio?

If so, you shouldn't as this isn't going to give you an accurate
representation of processes. When I compile your code into an .exe and run
that .exe directly with Task Manager open, Excel comes up and then drops off
the list after my method call to do the Excel stuff finishes and my console
sits open waiting for input via a Console.Read.

-Scott

"Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
news:29BA0CA5-F757-4233-B1ED-7DE4D13AC7B3@microsoft.com...
> Using the following as test code, the Excel process remains. Can you see
> a
> reason for this?
>
> Sub Test
> Dim objExcelApp As New Excel.Application
> Dim objExcelWorkBook As Excel.Workbook =
> objExcelApp.Workbooks.Open("C:\Test.xls")
> Dim objExcelWorksheet As Excel.Worksheet =
> objExcelWorkBook.Sheets(1)
> Dim objRange As Excel.Range
>
> objRange = objExcelWorksheet.Range("A1")
> MsgBox(objRange.Text)
>
> objExcelWorkBook.Close()
> objExcelApp.Quit()
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> objRange = Nothing
> objExcelWorksheet = Nothing
> objExcelWorkBook = Nothing
> objExcelApp = Nothing
> End Sub
> "Scott M." wrote:
>
>> You must call ReleaseComObject(obj) on EACH COM object you've created.
>>
>> That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
>> objects one might instantiate within the Excel.Application.
>>
>> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
>> news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
>> >I have a vb.net app that opens an excel worksheet, reads data and then
>> >closes
>> > the sheet. Im noticing that the Excel process is still running after I
>> > have
>> > closed and disposed of my excel objects.
>> >
>> > The following code (Test1) demonstrates the essence of what I am doing.
>> > When I check the processes while ruinning the method, I notice that the
>> > Excel
>> > process remains after exiting the sub (and until I exit the
>> > application)
>> >
>> > Sub Test1
>> > Dim objExcelApp As New Excel.Application
>> >
>> > Dim objExcelWorkBook As Excel.Workbook =
>> > objExcelApp.Workbooks.Open("C:\Test.xls")
>> > Dim objExcelWorksheet As Excel.Worksheet =
>> > objExcelWorkBook.Sheets(1)
>> >
>> > Dim objRange As Excel.Range
>> > objRange = objExcelWorksheet.Range("A1")
>> > MsgBox(objRange.Text)
>> > ''http://www.vbforums.com/archive/index.php/t-396405.html
>> > objRange = Nothing
>> > objExcelWorksheet = Nothing
>> > objExcelWorkBook.Close()
>> > objExcelWorkBook = Nothing
>> > objExcelApp.Workbooks.Close()
>> > objExcelApp.Quit()
>> >
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >
>> > objExcelApp = Nothing
>> >
>> > End Sub
>> >
>> > When I strip the code dow to this (Test2) I notice that the process is
>> > created on line
>> > Dim objExcelApp As New Excel.Application
>> > and killed on line
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >
>> > Sub Test2
>> > Dim objExcelApp As New Excel.Application
>> >
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >
>> > objExcelApp = Nothing
>> > End Sub
>> >
>> >
>> > What is it that is keeping a reference to the Excel process and how do
>> > I
>> > kill the process?
>>
>>
>>



Re: Excel does not die using automation by MSDNNospam248

MSDNNospam248
Mon Jul 07 04:54:02 CDT 2008

I was doing this in debug mode. I have done as you suggested and run the
compiled exe (winforms). The excel process is still running after executing
the code. The process is only killed when the form is closed. I have no
other code on this form other than a button_click event to execute the code.

I then tried the same thing as console app as you have done and in my case
the excel process is running until the console window closes

Imports Microsoft.Office.Interop

Module Module1

Sub Main()
Dim objExcelApp As New Excel.Application
Dim objExcelWorkBook As Excel.Workbook =
objExcelApp.Workbooks.Open("C:\Test.xls")
Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
Dim objRange As Excel.Range

objRange = objExcelWorksheet.Range("A1")
Console.WriteLine(objRange.Text)
'Console.Read()


objExcelWorkBook.Close()
objExcelApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)

objRange = Nothing
objExcelWorksheet = Nothing
objExcelWorkBook = Nothing
objExcelApp = Nothing

Console.WriteLine("Waiting")
Console.Read()
End Sub

End Module
"Scott M." wrote:

> Are you checking to see if Excel is running when you are debugging your code
> in Visual Studio?
>
> If so, you shouldn't as this isn't going to give you an accurate
> representation of processes. When I compile your code into an .exe and run
> that .exe directly with Task Manager open, Excel comes up and then drops off
> the list after my method call to do the Excel stuff finishes and my console
> sits open waiting for input via a Console.Read.
>
> -Scott
>
> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> news:29BA0CA5-F757-4233-B1ED-7DE4D13AC7B3@microsoft.com...
> > Using the following as test code, the Excel process remains. Can you see
> > a
> > reason for this?
> >
> > Sub Test
> > Dim objExcelApp As New Excel.Application
> > Dim objExcelWorkBook As Excel.Workbook =
> > objExcelApp.Workbooks.Open("C:\Test.xls")
> > Dim objExcelWorksheet As Excel.Worksheet =
> > objExcelWorkBook.Sheets(1)
> > Dim objRange As Excel.Range
> >
> > objRange = objExcelWorksheet.Range("A1")
> > MsgBox(objRange.Text)
> >
> > objExcelWorkBook.Close()
> > objExcelApp.Quit()
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >
> > objRange = Nothing
> > objExcelWorksheet = Nothing
> > objExcelWorkBook = Nothing
> > objExcelApp = Nothing
> > End Sub
> > "Scott M." wrote:
> >
> >> You must call ReleaseComObject(obj) on EACH COM object you've created.
> >>
> >> That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
> >> objects one might instantiate within the Excel.Application.
> >>
> >> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> >> news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
> >> >I have a vb.net app that opens an excel worksheet, reads data and then
> >> >closes
> >> > the sheet. Im noticing that the Excel process is still running after I
> >> > have
> >> > closed and disposed of my excel objects.
> >> >
> >> > The following code (Test1) demonstrates the essence of what I am doing.
> >> > When I check the processes while ruinning the method, I notice that the
> >> > Excel
> >> > process remains after exiting the sub (and until I exit the
> >> > application)
> >> >
> >> > Sub Test1
> >> > Dim objExcelApp As New Excel.Application
> >> >
> >> > Dim objExcelWorkBook As Excel.Workbook =
> >> > objExcelApp.Workbooks.Open("C:\Test.xls")
> >> > Dim objExcelWorksheet As Excel.Worksheet =
> >> > objExcelWorkBook.Sheets(1)
> >> >
> >> > Dim objRange As Excel.Range
> >> > objRange = objExcelWorksheet.Range("A1")
> >> > MsgBox(objRange.Text)
> >> > ''http://www.vbforums.com/archive/index.php/t-396405.html
> >> > objRange = Nothing
> >> > objExcelWorksheet = Nothing
> >> > objExcelWorkBook.Close()
> >> > objExcelWorkBook = Nothing
> >> > objExcelApp.Workbooks.Close()
> >> > objExcelApp.Quit()
> >> >
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >
> >> > objExcelApp = Nothing
> >> >
> >> > End Sub
> >> >
> >> > When I strip the code dow to this (Test2) I notice that the process is
> >> > created on line
> >> > Dim objExcelApp As New Excel.Application
> >> > and killed on line
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >
> >> > Sub Test2
> >> > Dim objExcelApp As New Excel.Application
> >> >
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >
> >> > objExcelApp = Nothing
> >> > End Sub
> >> >
> >> >
> >> > What is it that is keeping a reference to the Excel process and how do
> >> > I
> >> > kill the process?
> >>
> >>
> >>
>
>
>

Re: Excel does not die using automation by Scott

Scott
Mon Jul 07 05:58:42 CDT 2008

What version of .NET are you using? Excel closes in my console app when I
try.


"Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
news:BE108C26-9B16-46B8-B343-B02E2EFDA1FE@microsoft.com...
>I was doing this in debug mode. I have done as you suggested and run the
> compiled exe (winforms). The excel process is still running after
> executing
> the code. The process is only killed when the form is closed. I have no
> other code on this form other than a button_click event to execute the
> code.
>
> I then tried the same thing as console app as you have done and in my case
> the excel process is running until the console window closes
>
> Imports Microsoft.Office.Interop
>
> Module Module1
>
> Sub Main()
> Dim objExcelApp As New Excel.Application
> Dim objExcelWorkBook As Excel.Workbook =
> objExcelApp.Workbooks.Open("C:\Test.xls")
> Dim objExcelWorksheet As Excel.Worksheet =
> objExcelWorkBook.Sheets(1)
> Dim objRange As Excel.Range
>
> objRange = objExcelWorksheet.Range("A1")
> Console.WriteLine(objRange.Text)
> 'Console.Read()
>
>
> objExcelWorkBook.Close()
> objExcelApp.Quit()
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> objRange = Nothing
> objExcelWorksheet = Nothing
> objExcelWorkBook = Nothing
> objExcelApp = Nothing
>
> Console.WriteLine("Waiting")
> Console.Read()
> End Sub
>
> End Module
> "Scott M." wrote:
>
>> Are you checking to see if Excel is running when you are debugging your
>> code
>> in Visual Studio?
>>
>> If so, you shouldn't as this isn't going to give you an accurate
>> representation of processes. When I compile your code into an .exe and
>> run
>> that .exe directly with Task Manager open, Excel comes up and then drops
>> off
>> the list after my method call to do the Excel stuff finishes and my
>> console
>> sits open waiting for input via a Console.Read.
>>
>> -Scott
>>
>> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
>> news:29BA0CA5-F757-4233-B1ED-7DE4D13AC7B3@microsoft.com...
>> > Using the following as test code, the Excel process remains. Can you
>> > see
>> > a
>> > reason for this?
>> >
>> > Sub Test
>> > Dim objExcelApp As New Excel.Application
>> > Dim objExcelWorkBook As Excel.Workbook =
>> > objExcelApp.Workbooks.Open("C:\Test.xls")
>> > Dim objExcelWorksheet As Excel.Worksheet =
>> > objExcelWorkBook.Sheets(1)
>> > Dim objRange As Excel.Range
>> >
>> > objRange = objExcelWorksheet.Range("A1")
>> > MsgBox(objRange.Text)
>> >
>> > objExcelWorkBook.Close()
>> > objExcelApp.Quit()
>> >
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
>> >
>> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >
>> > objRange = Nothing
>> > objExcelWorksheet = Nothing
>> > objExcelWorkBook = Nothing
>> > objExcelApp = Nothing
>> > End Sub
>> > "Scott M." wrote:
>> >
>> >> You must call ReleaseComObject(obj) on EACH COM object you've created.
>> >>
>> >> That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
>> >> objects one might instantiate within the Excel.Application.
>> >>
>> >> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
>> >> news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
>> >> >I have a vb.net app that opens an excel worksheet, reads data and
>> >> >then
>> >> >closes
>> >> > the sheet. Im noticing that the Excel process is still running
>> >> > after I
>> >> > have
>> >> > closed and disposed of my excel objects.
>> >> >
>> >> > The following code (Test1) demonstrates the essence of what I am
>> >> > doing.
>> >> > When I check the processes while ruinning the method, I notice that
>> >> > the
>> >> > Excel
>> >> > process remains after exiting the sub (and until I exit the
>> >> > application)
>> >> >
>> >> > Sub Test1
>> >> > Dim objExcelApp As New Excel.Application
>> >> >
>> >> > Dim objExcelWorkBook As Excel.Workbook =
>> >> > objExcelApp.Workbooks.Open("C:\Test.xls")
>> >> > Dim objExcelWorksheet As Excel.Worksheet =
>> >> > objExcelWorkBook.Sheets(1)
>> >> >
>> >> > Dim objRange As Excel.Range
>> >> > objRange = objExcelWorksheet.Range("A1")
>> >> > MsgBox(objRange.Text)
>> >> > ''http://www.vbforums.com/archive/index.php/t-396405.html
>> >> > objRange = Nothing
>> >> > objExcelWorksheet = Nothing
>> >> > objExcelWorkBook.Close()
>> >> > objExcelWorkBook = Nothing
>> >> > objExcelApp.Workbooks.Close()
>> >> > objExcelApp.Quit()
>> >> >
>> >> >
>> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >> >
>> >> > objExcelApp = Nothing
>> >> >
>> >> > End Sub
>> >> >
>> >> > When I strip the code dow to this (Test2) I notice that the process
>> >> > is
>> >> > created on line
>> >> > Dim objExcelApp As New Excel.Application
>> >> > and killed on line
>> >> >
>> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >> >
>> >> > Sub Test2
>> >> > Dim objExcelApp As New Excel.Application
>> >> >
>> >> >
>> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>> >> >
>> >> > objExcelApp = Nothing
>> >> > End Sub
>> >> >
>> >> >
>> >> > What is it that is keeping a reference to the Excel process and how
>> >> > do
>> >> > I
>> >> > kill the process?
>> >>
>> >>
>> >>
>>
>>
>>



Re: Excel does not die using automation by MSDNNospam248

MSDNNospam248
Mon Jul 07 07:38:03 CDT 2008

im using .net 2.0

"Scott M." wrote:

> What version of .NET are you using? Excel closes in my console app when I
> try.
>
>
> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> news:BE108C26-9B16-46B8-B343-B02E2EFDA1FE@microsoft.com...
> >I was doing this in debug mode. I have done as you suggested and run the
> > compiled exe (winforms). The excel process is still running after
> > executing
> > the code. The process is only killed when the form is closed. I have no
> > other code on this form other than a button_click event to execute the
> > code.
> >
> > I then tried the same thing as console app as you have done and in my case
> > the excel process is running until the console window closes
> >
> > Imports Microsoft.Office.Interop
> >
> > Module Module1
> >
> > Sub Main()
> > Dim objExcelApp As New Excel.Application
> > Dim objExcelWorkBook As Excel.Workbook =
> > objExcelApp.Workbooks.Open("C:\Test.xls")
> > Dim objExcelWorksheet As Excel.Worksheet =
> > objExcelWorkBook.Sheets(1)
> > Dim objRange As Excel.Range
> >
> > objRange = objExcelWorksheet.Range("A1")
> > Console.WriteLine(objRange.Text)
> > 'Console.Read()
> >
> >
> > objExcelWorkBook.Close()
> > objExcelApp.Quit()
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
> >
> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >
> > objRange = Nothing
> > objExcelWorksheet = Nothing
> > objExcelWorkBook = Nothing
> > objExcelApp = Nothing
> >
> > Console.WriteLine("Waiting")
> > Console.Read()
> > End Sub
> >
> > End Module
> > "Scott M." wrote:
> >
> >> Are you checking to see if Excel is running when you are debugging your
> >> code
> >> in Visual Studio?
> >>
> >> If so, you shouldn't as this isn't going to give you an accurate
> >> representation of processes. When I compile your code into an .exe and
> >> run
> >> that .exe directly with Task Manager open, Excel comes up and then drops
> >> off
> >> the list after my method call to do the Excel stuff finishes and my
> >> console
> >> sits open waiting for input via a Console.Read.
> >>
> >> -Scott
> >>
> >> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> >> news:29BA0CA5-F757-4233-B1ED-7DE4D13AC7B3@microsoft.com...
> >> > Using the following as test code, the Excel process remains. Can you
> >> > see
> >> > a
> >> > reason for this?
> >> >
> >> > Sub Test
> >> > Dim objExcelApp As New Excel.Application
> >> > Dim objExcelWorkBook As Excel.Workbook =
> >> > objExcelApp.Workbooks.Open("C:\Test.xls")
> >> > Dim objExcelWorksheet As Excel.Worksheet =
> >> > objExcelWorkBook.Sheets(1)
> >> > Dim objRange As Excel.Range
> >> >
> >> > objRange = objExcelWorksheet.Range("A1")
> >> > MsgBox(objRange.Text)
> >> >
> >> > objExcelWorkBook.Close()
> >> > objExcelApp.Quit()
> >> >
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
> >> >
> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >
> >> > objRange = Nothing
> >> > objExcelWorksheet = Nothing
> >> > objExcelWorkBook = Nothing
> >> > objExcelApp = Nothing
> >> > End Sub
> >> > "Scott M." wrote:
> >> >
> >> >> You must call ReleaseComObject(obj) on EACH COM object you've created.
> >> >>
> >> >> That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
> >> >> objects one might instantiate within the Excel.Application.
> >> >>
> >> >> "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> >> >> news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
> >> >> >I have a vb.net app that opens an excel worksheet, reads data and
> >> >> >then
> >> >> >closes
> >> >> > the sheet. Im noticing that the Excel process is still running
> >> >> > after I
> >> >> > have
> >> >> > closed and disposed of my excel objects.
> >> >> >
> >> >> > The following code (Test1) demonstrates the essence of what I am
> >> >> > doing.
> >> >> > When I check the processes while ruinning the method, I notice that
> >> >> > the
> >> >> > Excel
> >> >> > process remains after exiting the sub (and until I exit the
> >> >> > application)
> >> >> >
> >> >> > Sub Test1
> >> >> > Dim objExcelApp As New Excel.Application
> >> >> >
> >> >> > Dim objExcelWorkBook As Excel.Workbook =
> >> >> > objExcelApp.Workbooks.Open("C:\Test.xls")
> >> >> > Dim objExcelWorksheet As Excel.Worksheet =
> >> >> > objExcelWorkBook.Sheets(1)
> >> >> >
> >> >> > Dim objRange As Excel.Range
> >> >> > objRange = objExcelWorksheet.Range("A1")
> >> >> > MsgBox(objRange.Text)
> >> >> > ''http://www.vbforums.com/archive/index.php/t-396405.html
> >> >> > objRange = Nothing
> >> >> > objExcelWorksheet = Nothing
> >> >> > objExcelWorkBook.Close()
> >> >> > objExcelWorkBook = Nothing
> >> >> > objExcelApp.Workbooks.Close()
> >> >> > objExcelApp.Quit()
> >> >> >
> >> >> >
> >> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >> >
> >> >> > objExcelApp = Nothing
> >> >> >
> >> >> > End Sub
> >> >> >
> >> >> > When I strip the code dow to this (Test2) I notice that the process
> >> >> > is
> >> >> > created on line
> >> >> > Dim objExcelApp As New Excel.Application
> >> >> > and killed on line
> >> >> >
> >> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >> >
> >> >> > Sub Test2
> >> >> > Dim objExcelApp As New Excel.Application
> >> >> >
> >> >> >
> >> >> > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> >> >> >
> >> >> > objExcelApp = Nothing
> >> >> > End Sub
> >> >> >
> >> >> >
> >> >> > What is it that is keeping a reference to the Excel process and how
> >> >> > do
> >> >> > I
> >> >> > kill the process?
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>

Re: Excel does not die using automation by ZSvedic

ZSvedic
Mon Jul 07 10:29:47 CDT 2008

On Jul 6, 1:53=A0pm, Terry Holland <MSDNNospam...@nospam.nospam> wrote:
> I have a vb.net app that opens anexcelworksheet, reads data and then clos=
es
> the sheet. =A0Im noticing that theExcelprocess is still running after I h=
ave
> closed and disposed of myexcelobjects.
>
> The following code (Test1) demonstrates the essence of what I am doing. =
=A0
> When I check the processes while ruinning the method, I notice that theEx=
cel
> process remains after exiting the sub (and until I exit the application)
>
> Sub Test1
> =A0 =A0 =A0 =A0 =A0 =A0Dim objExcelApp As NewExcel.Application
>
> =A0 =A0 =A0 =A0 Dim objExcelWorkBook AsExcel.Workbook =3D
> objExcelApp.Workbooks.Open("C:\Test.xls")
> =A0 =A0 =A0 =A0 Dim objExcelWorksheet AsExcel.Worksheet =3D objExcelWorkB=
ook.Sheets(1)
>
> =A0 =A0 =A0 =A0 Dim objRange AsExcel.Range
> =A0 =A0 =A0 =A0 objRange =3D objExcelWorksheet.Range("A1")
> =A0 =A0 =A0 =A0 MsgBox(objRange.Text)
> =A0 =A0 =A0 =A0 ''http://www.vbforums.com/archive/index.php/t-396405.html
> =A0 =A0 =A0 =A0 objRange =3D Nothing
> =A0 =A0 =A0 =A0 objExcelWorksheet =3D Nothing
> =A0 =A0 =A0 =A0 objExcelWorkBook.Close()
> =A0 =A0 =A0 =A0 objExcelWorkBook =3D Nothing
> =A0 =A0 =A0 =A0 objExcelApp.Workbooks.Close()
> =A0 =A0 =A0 =A0 objExcelApp.Quit()
>
> =A0 =A0 =A0 =A0 System.Runtime.InteropServices.Marshal.ReleaseComObject(o=
bjExcelApp)
>
> =A0 =A0 =A0 =A0 objExcelApp =3D Nothing
>
> End Sub
>
> When I strip the code dow to this (Test2) I notice that the process is
> created on line
> =A0 =A0 =A0 =A0 =A0Dim objExcelApp As NewExcel.Application
> and killed on line
> =A0 =A0 =A0 =A0 System.Runtime.InteropServices.Marshal.ReleaseComObject(o=
bjExcelApp)
>
> Sub Test2
> =A0 =A0 =A0 =A0 Dim objExcelApp As NewExcel.Application
>
> =A0 =A0 =A0 =A0 System.Runtime.InteropServices.Marshal.ReleaseComObject(o=
bjExcelApp)
>
> =A0 =A0 =A0 =A0 objExcelApp =3D Nothing
> End Sub
>
> What is it that is keeping a reference to theExcelprocess and how do I
> kill the process?

I don't know if it works for you, but if you have small workbooks you
can use
our GemBox.Spreadsheet Free (http://www.gemboxsoftware.com/
GBSpreadsheetFree.htm)
Excel component for XLS/CSV/XLSX reading/writing/reporting.
Automation has many issues: http://www.gemboxsoftware.com/GBSpreadsheet.htm=
#Automation

--Zeljko

Re: Excel does not die using automation by JeffWinn

JeffWinn
Mon Jul 07 13:10:01 CDT 2008

It looks to me like you've created a race condition. You need to call
objExcelApp.Quit() after you've released all of the COM objects you've been
using (except for the excel application instance), not before.

Also, as you've done below make sure you release the objects inversely from
the order they were instantiated. The last object you created becomes the
first object you're releasing.

Other than that, your application looks fine to me.

"Terry Holland" wrote:

> Using the following as test code, the Excel process remains. Can you see a
> reason for this?
>
> Sub Test
> Dim objExcelApp As New Excel.Application
> Dim objExcelWorkBook As Excel.Workbook =
> objExcelApp.Workbooks.Open("C:\Test.xls")
> Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
> Dim objRange As Excel.Range
>
> objRange = objExcelWorksheet.Range("A1")
> MsgBox(objRange.Text)
>
> objExcelWorkBook.Close()
> objExcelApp.Quit()
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorksheet)
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWorkBook)
> System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
>
> objRange = Nothing
> objExcelWorksheet = Nothing
> objExcelWorkBook = Nothing
> objExcelApp = Nothing
> End Sub
> "Scott M." wrote:
>
> > You must call ReleaseComObject(obj) on EACH COM object you've created.
> >
> > That means on the Range, Worksheet, Workbook, Chart, PivotTable, etc.
> > objects one might instantiate within the Excel.Application.
> >
> > "Terry Holland" <MSDNNospam248@nospam.nospam> wrote in message
> > news:9B590AD1-E4A3-4EA8-84AA-8A0358F3673A@microsoft.com...
> > >I have a vb.net app that opens an excel worksheet, reads data and then
> > >closes
> > > the sheet. Im noticing that the Excel process is still running after I
> > > have
> > > closed and disposed of my excel objects.
> > >
> > > The following code (Test1) demonstrates the essence of what I am doing.
> > > When I check the processes while ruinning the method, I notice that the
> > > Excel
> > > process remains after exiting the sub (and until I exit the application)
> > >
> > > Sub Test1
> > > Dim objExcelApp As New Excel.Application
> > >
> > > Dim objExcelWorkBook As Excel.Workbook =
> > > objExcelApp.Workbooks.Open("C:\Test.xls")
> > > Dim objExcelWorksheet As Excel.Worksheet =
> > > objExcelWorkBook.Sheets(1)
> > >
> > > Dim objRange As Excel.Range
> > > objRange = objExcelWorksheet.Range("A1")
> > > MsgBox(objRange.Text)
> > > ''http://www.vbforums.com/archive/index.php/t-396405.html
> > > objRange = Nothing
> > > objExcelWorksheet = Nothing
> > > objExcelWorkBook.Close()
> > > objExcelWorkBook = Nothing
> > > objExcelApp.Workbooks.Close()
> > > objExcelApp.Quit()
> > >
> > >
> > > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> > >
> > > objExcelApp = Nothing
> > >
> > > End Sub
> > >
> > > When I strip the code dow to this (Test2) I notice that the process is
> > > created on line
> > > Dim objExcelApp As New Excel.Application
> > > and killed on line
> > >
> > > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> > >
> > > Sub Test2
> > > Dim objExcelApp As New Excel.Application
> > >
> > >
> > > System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
> > >
> > > objExcelApp = Nothing
> > > End Sub
> > >
> > >
> > > What is it that is keeping a reference to the Excel process and how do I
> > > kill the process?
> >
> >
> >

Re: Excel does not die using automation by Scott

Scott
Mon Jul 07 16:19:54 CDT 2008


"Jeff Winn" <JeffWinn@discussions.microsoft.com> wrote in message
news:6AEC2118-C87D-4EDA-9560-AE84337F107A@microsoft.com...
> It looks to me like you've created a race condition. You need to call
> objExcelApp.Quit() after you've released all of the COM objects you've
> been
> using (except for the excel application instance), not before.

I don't think this makes any difference whatsoever. The fact is that if you
explicitly create Excel objects and explictly release them (in the opposite
order you created them), you should have no trouble calling
ReleaseComObject() After quitting Excel.

As I said, I have the code working just fine:


Imports System.Runtime.InteropServices

Sub Test()
Dim objExcelApp As New Excel.Application
Dim objExcelWorkBook As Excel.Workbook =
objExcelApp.Workbooks.Open("C:\test.xls")
Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
Dim objRange As Excel.Range = objExcelWorksheet.Range("A1")
objExcelApp.Visible = True
objExcelWorkBook.Close()
objExcelApp.Quit()

Marshal.ReleaseComObject(objRange)
Marshal.ReleaseComObject(objExcelWorksheet)
Marshal.ReleaseComObject(objExcelWorkBook)
Marshal.ReleaseComObject(objExcelApp)

objRange = Nothing
objExcelWorksheet = Nothing
objExcelWorkBook = Nothing
objExcelApp = Nothing
End Sub



Re: Excel does not die using automation by Jeff

Jeff
Mon Jul 07 17:54:13 CDT 2008

You don't think? The only reason it's working is because the process that
started the excel application instance has ended. Read a line from the
console in that test application you've written so the thread waits for
input and check your process list in task manager. Excel will not have
closed the process correctly. When Quit gets called, Excel should terminate
even if the application that started it is still open - if it doesn't
something is wrong.

I had a huge problem with this doing server side automation about 4 months
ago with the website (I know, you're not supposed to do it - we had no
choice in the matter) I was working on created a huge memory leak because I
wasn't releasing the COM objects correctly and Excel wasn't terminating.

The objects must be released before the Quit method is called on the excel
instance, otherwise the Excel process is going to hang in limbo until the
process that started it closes. In my case since it was a website they
wouldn't ever close, thus the memory leak I had to find.

"Scott M." <s-mar@nospam.nospam> wrote in message
news:uvmpncH4IHA.4448@TK2MSFTNGP05.phx.gbl...
>
> "Jeff Winn" <JeffWinn@discussions.microsoft.com> wrote in message
> news:6AEC2118-C87D-4EDA-9560-AE84337F107A@microsoft.com...
>> It looks to me like you've created a race condition. You need to call
>> objExcelApp.Quit() after you've released all of the COM objects you've
>> been
>> using (except for the excel application instance), not before.
>
> I don't think this makes any difference whatsoever. The fact is that if
> you explicitly create Excel objects and explictly release them (in the
> opposite order you created them), you should have no trouble calling
> ReleaseComObject() After quitting Excel.
>
> As I said, I have the code working just fine:
>
>
> Imports System.Runtime.InteropServices
>
> Sub Test()
> Dim objExcelApp As New Excel.Application
> Dim objExcelWorkBook As Excel.Workbook =
> objExcelApp.Workbooks.Open("C:\test.xls")
> Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
> Dim objRange As Excel.Range = objExcelWorksheet.Range("A1")
> objExcelApp.Visible = True
> objExcelWorkBook.Close()
> objExcelApp.Quit()
>
> Marshal.ReleaseComObject(objRange)
> Marshal.ReleaseComObject(objExcelWorksheet)
> Marshal.ReleaseComObject(objExcelWorkBook)
> Marshal.ReleaseComObject(objExcelApp)
>
> objRange = Nothing
> objExcelWorksheet = Nothing
> objExcelWorkBook = Nothing
> objExcelApp = Nothing
> End Sub
>


Re: Excel does not die using automation by Scott

Scott
Mon Jul 07 19:51:25 CDT 2008

"Jeff Winn" <jwinn@nospam.com> wrote in message
news:3FAD1370-EFDB-4EEE-93A1-55F70DFD5852@microsoft.com...
> You don't think? The only reason it's working is because the process that
> started the excel application instance has ended. Read a line from the
> console in that test application you've written so the thread waits for
> input and check your process list in task manager. Excel will not have
> closed the process correctly.

I don't think you've read my posts. I do have a Console.ReadLine() in there
for just that purpose and Excel has been terminated.

> When Quit gets called, Excel should terminate even if the application that
> started it is still open - if it doesn't something is wrong.

No, Excel should terminate when all the ReleaseComObject() calls have
completed, not when .Quit() is called. Since the ReleaseComObject() calls
come AFTER quit (as they should), you won't see Excel's process actually
terminate until then. What you will see immediately after Quit is the Excel
UI shutdown.

>
> I had a huge problem with this doing server side automation about 4 months
> ago with the website (I know, you're not supposed to do it - we had no
> choice in the matter) I was working on created a huge memory leak because
> I wasn't releasing the COM objects correctly and Excel wasn't terminating.
>
> The objects must be released before the Quit method is called on the excel
> instance, otherwise the Excel process is going to hang in limbo until the
> process that started it closes. In my case since it was a website they
> wouldn't ever close, thus the memory leak I had to find.

Again, I'm telling you that you calling RelaseComObject() prior to calling
Quit() will not have any bearing on Excel terminating.

In order for Excel to terminate, the CLR must relase all of its references
to the unmanaged object. It does that when you call ReleaseComObject(obj).
Now, you certainly wouldn't want to call that while your managed code is
still using your Runtime Callable Wrappers, so you hold off on calling
ReleaseComObject(obj) until you are DONE using your .NET object references.

So this:

objExcelApp.Quit()

Marshal.ReleaseComObject(objRange)
Marshal.ReleaseComObject(objExcelWorksheet)
Marshal.ReleaseComObject(objExcelWorkBook)
Marshal.ReleaseComObject(objExcelApp)

or this:

Marshal.ReleaseComObject(objRange)
Marshal.ReleaseComObject(objExcelWorksheet)
Marshal.ReleaseComObject(objExcelWorkBook)

objExcelApp.Quit()
Marshal.ReleaseComObject(objExcelApp)

Won't make any difference because it's not the Quit() that actually shuts
down Excel's process, it's shut down when there are zero references to the
COM object, and that only occurs via ReleaseComObject(obj), not Quit().

Again, the code I've shown works if you run it outside the debugger
(console.readline and all).

-Scott


>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:uvmpncH4IHA.4448@TK2MSFTNGP05.phx.gbl...
>>
>> "Jeff Winn" <JeffWinn@discussions.microsoft.com> wrote in message
>> news:6AEC2118-C87D-4EDA-9560-AE84337F107A@microsoft.com...
>>> It looks to me like you've created a race condition. You need to call
>>> objExcelApp.Quit() after you've released all of the COM objects you've
>>> been
>>> using (except for the excel application instance), not before.
>>
>> I don't think this makes any difference whatsoever. The fact is that if
>> you explicitly create Excel objects and explictly release them (in the
>> opposite order you created them), you should have no trouble calling
>> ReleaseComObject() After quitting Excel.
>>
>> As I said, I have the code working just fine:
>>
>>
>> Imports System.Runtime.InteropServices
>>
>> Sub Test()
>> Dim objExcelApp As New Excel.Application
>> Dim objExcelWorkBook As Excel.Workbook =
>> objExcelApp.Workbooks.Open("C:\test.xls")
>> Dim objExcelWorksheet As Excel.Worksheet = objExcelWorkBook.Sheets(1)
>> Dim objRange As Excel.Range = objExcelWorksheet.Range("A1")
>> objExcelApp.Visible = True
>> objExcelWorkBook.Close()
>> objExcelApp.Quit()
>>
>> Marshal.ReleaseComObject(objRange)
>> Marshal.ReleaseComObject(objExcelWorksheet)
>> Marshal.ReleaseComObject(objExcelWorkBook)
>> Marshal.ReleaseComObject(objExcelApp)
>>
>> objRange = Nothing
>> objExcelWorksheet = Nothing
>> objExcelWorkBook = Nothing
>> objExcelApp = Nothing
>> End Sub
>>
>