vb.net - Using Application.Intersect why do I get Type Mismatch -
here code. thought should work; i'm saving wb , trying excel application object using wb.application. works xlapp.intersect fails "type mismatch" error.
dim wb object sub first() dim xlapp object xlapp = createobject("excel.application") wb = myxl.xlapp.workbooks.add() end sub sub second() dim xlapp object = wb.application dim rg object = xlapp.intersect(ws.usedrange, ws.columns("b")) end sub
is possible excel application workbook , still use intersect method?
i tried saving xlapp global variable , re-using it, didn't work either; same error.
i trying avoid using excel interop reference maintain backward compatibility old versions of excel.
msdn library _application.intersect method
i noticed application object of type: microsoft.office.interop.excel.applicationclass
the varocarbas answer got me thinking various types using. experimented various combinations of objects dim objrange1 object
, declared types dim rg1 excel.range
here code tested , works fine except 1 @ end gave me "type mismatch" error.
sub testxl1() dim xlapp object dim wb object dim ws object xlapp = createobject("excel.application") xlapp.visible = true wb = xlapp.workbooks.add() xlapp = wb.application ws = wb.worksheets(1) ws.cells(1, 1) = "first" ws.cells(2, 2) = "hello" 'this works fine dim rg1 microsoft.office.interop.excel.range dim rg2 microsoft.office.interop.excel.range dim rg3 microsoft.office.interop.excel.range rg1 = ws.usedrange rg2 = ws.columns("b") rg3 = xlapp.intersect(rg1, rg2) rg3.select() 'this works fine dim rg4 object rg4 = xlapp.intersect(rg1, rg2) 'public member 'intersect' on type 'object()' not found. rg4.select() 'this works fine dim objrange1 object = ws.usedrange dim objrange2 object = ws.columns("b") dim rg5 microsoft.office.interop.excel.range rg5 = xlapp.intersect(objrange1, objrange2) rg5.select() 'this works fine dim objrange3 object objrange3 = xlapp.intersect(objrange1, objrange2) objrange3.select() 'this give type mismatch error objrange3 = xlapp.intersect(ws.usedrange, ws.columns("b")) objrange3.select() end sub
so guess answer set ranges object variables before using them parameters in objrange3 = xlapp.intersect(objrange1, objrange2)
varocarbas answer had using directcast
rg = directcast(xlapp, microsoft.office.interop.excel.application).intersect(...
understanding excel object model .net developer's perspective
Comments
Post a Comment