vb.net - pastespecial Crashing when pasting excel data -
i using following code data several excel files , paste them single new file:
= 0 amountoffiles - 1 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 currentfile string = strfilenames(i) dim intamountofrows integer = amountofrows(currentfile) dim intstartofemptyrow int16 = amountofrows(summarylocation) '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 xlsheetsource.range("a2:k" & intamountofrows).copy() xlsheettarget.range("a2:k" & intstartofemptyrow).pastespecial(excel.xlpastetype.xlpasteall, excel.xlpastespecialoperation.xlpastespecialoperationnone, false, false) 'set focus on summary excelsheet xlwbtarget = xlappsource.workbooks.open(summarylocation) xlsheetsource = xlwbtarget.activesheet 'close excel xlwbsource.close(true) xlwbtarget.close(true) xlappsource.quit() xlapptarget.quit() 'cleanup xlappsource = nothing xlapptarget = nothing xlwbsource = nothing xlwbtarget = nothing xlsheetsource = nothing xlsheettarget = nothing next
however, when execute code, throws following error:
"method pastespecial of class range has failed"
wich points line:
xlsheettarget.range("a2:k" & intstartofemptyrow).pastespecial(excel.xlpastetype.xlpasteall, excel.xlpastespecialoperation.xlpastespecialoperationnone, false, false)
i have no idea how solve this, seeing pretty general error, googling gives me next none answers.
i have found way want, don't know why. not accept own answer, wait explain it.
the code used is:
'copy range of data source target file xlsheetsource.range("a2:k" & intamountofrows).copy() xlsheettarget.range("a" & intstartofemptyrow).pastespecial(excel.xlpastetype.xlpastevalues)
Comments
Post a Comment