简体   繁体   English

使用Application.Intersect为什么我会得到类型不匹配

[英]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.

相关问题 为什么我从代码中收到此错误,原因是条件表达式中的数据类型不匹配 - why i get this error from my code its say the data type mismatch in criteria expression 我应该在“条件表达式中的数据类型不匹配”中做什么 - what should I do in “Data type mismatch in criteria expression” 如何使用SQL更新来更新访问中的特定行? 在当前代码中,我不断收到错误数据类型不匹配的信息 - How do I update a specific row in access using SQL update? In my current code I keep getting the error data type mismatch 为什么使用 ExecuteNonQuery() 会出现“数据类型转换错误”? - Why do I get a “data type conversion error” with ExecuteNonQuery()? 如何更改数据类型以解决条件表达式中的数据类型不匹配 - How do I change data type to fix data type mismatch in criteria expression 当应用程序成功运行时,为什么会出现 x64 应用程序的编译错误 - Why do I get Compile Errors of x64 Application,when the application runs successfully 为什么在编译时我的VB.Net应用程序中不断显示“未定义类型”? - Why do I keep getting a “type is not defined” in my VB.Net application at compile time? 使用.NET DLL的经典ASP类型不匹配 - Classic ASP Type Mismatch using .NET DLL 为什么在ASP .NET应用程序中单击ordrer按钮时出现以下错误? - Why do I get the following error when clicking the ordrer button in my ASP .NET application? 使用GetStringValue fn时键入不匹配错误 - Type mismatch error while using GetStringValue fn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM