[英]Using Application.Intersect why do I get Type Mismatch
Here is my code. 这是我的代码。 I thought this should work; 我认为这应该起作用; I'm saving the wb and trying to get the Excel Application object by using wb.Application. 我正在保存wb,并尝试通过使用wb.Application获取Excel Application对象。 It actually works but XLApp.Intersect fails with "Type Mismatch" error. 它实际上可以工作,但是XLApp.Intersect失败,并显示“类型不匹配”错误。
Dim wb As Object
Sub First()
Dim XLApp As Object
XLApp = CreateObject("Excel.Application")
wb = myXL.XLApp.Workbooks.Add()
End Sub
Sub Second()
Dim XLApp as object = wb.Application
Dim rg as object = XLApp.Intersect(ws.UsedRange, ws.Columns("B"))
End Sub
Is it possible to get Excel Application from the workbook and still use the Intersect method? 是否可以从工作簿中获取Excel Application并仍使用Intersect方法?
I also tried saving the XLapp to a global variable and re-using it, but that didn't work either; 我还尝试将XLapp保存到全局变量并重新使用它,但这也不起作用。 same error. 同样的错误。
I am trying to avoid using an Excel Interop reference to maintain backward compatibility with old versions of Excel. 我试图避免使用Excel Interop引用来保持与旧版本Excel的向后兼容性。
MSDN Library _Application.Intersect Method MSDN Library _Application.Intersect方法
I noticed that my application object is of type: Microsoft.Office.Interop.Excel.ApplicationClass 我注意到我的应用程序对象的类型是:Microsoft.Office.Interop.Excel.ApplicationClass
The varocarbas answer got me to thinking about the various types I was using. Varocarbas的答案使我开始思考所使用的各种类型。 So I experimented with various combinations of objects Dim ObjRange1 As Object
and declared types Dim rg1 As Excel.Range
所以我尝试了将对象Dim ObjRange1 As Object
各种组合,并声明了Dim rg1 As Excel.Range
类型。
Here is the code that I tested and it works fine except the one at the end which gave me the "Type Mismatch" error. 这是我测试过的代码,除了末尾的代码使我出现“ Type Mismatch”错误之外,它都可以正常工作。
Sub testXL1()
Dim XLApp As Object
Dim wb As Object
Dim ws As 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 As Microsoft.Office.Interop.Excel.Range
Dim rg2 As Microsoft.Office.Interop.Excel.Range
Dim rg3 As Microsoft.Office.Interop.Excel.Range
rg1 = ws.usedRange
rg2 = ws.columns("B")
rg3 = XLApp.Intersect(rg1, rg2)
rg3.Select()
'this works fine
Dim rg4 As Object
rg4 = XLApp.Intersect(rg1, rg2) 'Public member 'Intersect' on type 'Object()' not found.
rg4.Select()
'this works fine
Dim ObjRange1 As Object = ws.usedRange
Dim ObjRange2 As Object = ws.columns("B")
Dim rg5 As Microsoft.Office.Interop.Excel.Range
rg5 = XLApp.Intersect(ObjRange1, ObjRange2)
rg5.Select()
'this works fine
Dim ObjRange3 As Object
ObjRange3 = XLApp.Intersect(ObjRange1, ObjRange2)
ObjRange3.Select()
'this give a Type Mismatch error
ObjRange3 = XLApp.Intersect(ws.usedRange, ws.columns("B"))
ObjRange3.Select()
End Sub
So I guess the answer is to set the ranges to object variables before using them as parameters in ObjRange3 = XLApp.Intersect(ObjRange1, ObjRange2)
所以我想答案是在对象变量用作ObjRange3 = XLApp.Intersect(ObjRange1, ObjRange2)
参数之前将范围设置为对象变量
varocarbas answer had to do with using DirectCast varocarbas答案与使用DirectCast有关
rg = DirectCast(XLApp, Microsoft.Office.Interop.Excel.Application).Intersect(...
Understanding the Excel Object Model from a .NET Developer's Perspective 从.NET开发人员的角度了解Excel对象模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.