Excel.Range object not disposing so not Closing the Excel process

有些话、适合烂在心里 提交于 2019-12-24 11:44:05

问题


I am banging my head since hours to dispose the Excel.Range Object creating by the statement below. I Have checked almost all the Discussions regarding it on Stack Overflow.

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

When i comment out this line Excel Application closes successfully. But when i use this line it creates the Excel.Range object which is not disposing of by GC. Can anyone please help me out.

Thanks


回答1:


You shouldn't do nested calls like that, instead change it to be something like:

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

Then r can be disposed properly by a:

Marshal.ReleaseComObject(r)

And then you can reuse the variable if you want:

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)
...


来源:https://stackoverflow.com/questions/7023973/excel-range-object-not-disposing-so-not-closing-the-excel-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!