vb.net - Unable to kill excel processes -
i using followiwing code copying , pasting excel files:
imports excel = microsoft.office.interop.excel imports system.io imports system.runtime.interopservices private sub btncombine_click(byval sender system.object, byval e system.eventargs) handles btncombine.click dim xlappsource new excel.application dim xlapptarget new excel.application dim xlwbsource excel.workbook dim xlwbtarget excel.workbook dim xlsheetsource excel.worksheet dim xlsheettarget excel.worksheet dim xlrangesource excel.range dim xlrangetarget excel.range dim progress integer = 0 pbrprogress.minimum = 0 pbrprogress.maximum = amountoffiles btncombine.enabled = false btnselect.enabled = false pbrprogress.visible = true getsavelocation() makeexcelfile() 'loop through excel files required data = 0 amountoffiles - 1 dim currentfile string = strfilenames(i) dim intamountofrows integer = amountofrows(currentfile) dim intstartofemptyrow integer = amountofrows(summarylocation) xlappsource.displayalerts = false xlapptarget.displayalerts = false 'set current workbook xlwbsource = xlappsource.workbooks.open(currentfile) xlwbtarget = xlapptarget.workbooks.open(summarylocation) 'set current worksheet xlsheetsource = xlwbsource.activesheet xlsheettarget = xlwbtarget.activesheet 'copy range of data source target file xlrangesource = xlsheetsource.range("a2:k" & intamountofrows) xlrangesource.copy() xlrangetarget = xlsheettarget.range("a" & intstartofemptyrow) xlrangetarget.pastespecial(excel.xlpastetype.xlpastevalues) 'save summary file before closing xlsheettarget.saveas(summarylocation) 'updating progress bar progress = progress + 1 pbrprogress.value = progress next 'close excel xlwbsource.close(true) xlwbtarget.close(true) xlappsource.quit() xlapptarget.quit() xlappsource.displayalerts = true xlapptarget.displayalerts = true 'cleanup marshal.releasecomobject(xlappsource) marshal.releasecomobject(xlapptarget) marshal.releasecomobject(xlwbsource) marshal.releasecomobject(xlwbtarget) marshal.releasecomobject(xlsheetsource) marshal.releasecomobject(xlsheettarget) marshal.releasecomobject(xlrangesource) marshal.releasecomobject(xlrangetarget) xlappsource = nothing xlapptarget = nothing xlwbsource = nothing xlwbtarget = nothing xlsheetsource = nothing xlsheettarget = nothing xlrangesource = nothing xlrangetarget = nothing msgbox("samenvoegen compleet") init() end sub
i have tried every solution given on so:
- never use 2 points on line
- i have tried using marshall.releasecomobject
- i have tried setting objects "nothing"
however, everytime run application, there 10-20 excel processes still running.
option explicit sub main() dim xlapp excel.application set xlapp = new excel.application xlapp.visible = false xlapp.displayalerts = false dim xlwb workbook set xlwb = xlapp.workbooks.open("c:\...\path") dim xlsht worksheet set xlsht = xlwb.sheets(1) xlsht.range("a1") = "message " & thisworkbook.fullname xlwb.saved = true xlwb.save xlwb.close xlapp.quit end sub
works me every single time , not leave excel processes hanging in task manager.
note: if code breaks @ point , not handle opened objects hang in processes tab in task manager. if haven't implemented error handling in code start here.
just consider alternative
option explicit sub main() on error goto errhandler dim xlapp excel.application set xlapp = new excel.application xlapp.visible = false xlapp.displayalerts = false dim xlwb workbook set xlwb = xlapp.workbooks.open("c:\...\path") dim xlsht worksheet set xlsht = xlwb.sheets(1) xlsht.range("a1") = "message " & thisworkbook.fullname xlwb.saved = true xlwb.save xlwb.close xlapp.quit exit sub errhandler: xlwb.saved = true xlwb.save xlwb.close xlapp.quit end sub
Comments
Post a Comment