Excel.Range object is not deleted, therefore does not terminate the Excel process

I'm banging my head off the clock to get rid of the Excel.Range object created according to the instruction below. I've checked almost all discussions regarding this for a stack overflow.

oSheet.Range("A1", "H1").Merge()

      

When I comment out this line, Excel exits successfully. But when I use this line, it creates an Excel.Range object that is not recycled by the GC. Can anyone please help me.

thank

+1


source to share


1 answer


You shouldn't be making nested calls like this, instead change it to something like:

Dim r As Object = oSheet.Range("A1", "H1")
r.Merge()

      

Then r can be correctly positioned a:



Marshal.ReleaseComObject(r)

      

And then you can reuse the variable if you like:

r = oSheet.Cells(TitleRow3, 1)
' do something with r
Marshal.ReleaseComObject(r)
r = 'get a new range from somewhere
' do something with r
Marshal.ReleaseComObject(r)
...

      

+1


source







All Articles