验证选择范围

时间:2019-02-18 17:27:55

标签: c# excel vb.net vsto

在Excel(以及其他Office应用程序)中,Globals.ThisAddIn.Application.Selection的名称将不同Type,具体取决于用户选择的内容:单元格,图表等。

如何验证用户选择的单元格?

If Globals.ThisAddIn.Application.Selection.GetType Is GetType(Excel.Range) then不起作用。问题似乎出在GetType(Excel.Range)上,它返回:

  

错误BC30560:名称空间中的“范围”不明确   “ Microsoft.Office.Interop.Excel”

有什么想法吗?


更新

已将源代码更改为以下代码,以避免命名空间含糊不清(如果有人知道此处的命名空间有冲突...?)

If Not Globals.ThisAddIn.Application.Selection.GetType Is _
   GetType(Global.Microsoft.Office.Interop.Excel.Range) Then

但是,当选择单元格时,If语句仍然无法正确运行。 立即窗口告诉我:

?GetType(global.Microsoft.Office.Interop.Excel.Range)
{Name = "Range" FullName = "Microsoft.Office.Interop.Excel.Range"}
    [Assembly]: {MyExcelVSTO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
    AssemblyQualifiedName: "Microsoft.Office.Interop.Excel.Range, MyExcelVSTO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    Attributes: Public Or ClassSemanticsMask Or Abstract Or Import {4257}
    BaseType: Nothing
    ContainsGenericParameters: False
    CustomAttributes: Count = 6
    DeclaredConstructors: {Length=0}
    DeclaredEvents: {Length=0}
    DeclaredFields: {Length=0}
    DeclaredMembers: {Length=12}
    DeclaredMethods: {Length=7}
    DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__23}
    DeclaredProperties: {Length=5}
    DeclaringMethod: 'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range), System.RuntimeType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
    DeclaringType: Nothing
    FullName: "Microsoft.Office.Interop.Excel.Range"
    GUID: {00020846-0000-0000-c000-000000000046}
    GenericParameterAttributes: 'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range), System.RuntimeType).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: 'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range), System.RuntimeType).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
    GenericTypeArguments: {Length=0}
    GenericTypeParameters: {Length=0}
    HasElementType: False
    ImplementedInterfaces: {Length=1}
    IsAbstract: True
    IsAnsiClass: True
    IsArray: False
    IsAutoClass: False
    IsAutoLayout: True
    IsByRef: False
    IsCOMObject: False
    IsClass: False
    IsConstructedGenericType: False
    IsContextful: False
    IsEnum: False
    IsExplicitLayout: False
    IsGenericParameter: False
    IsGenericType: False
    IsGenericTypeDefinition: False
    IsImport: True
    IsInterface: True
    IsLayoutSequential: False
    IsMarshalByRef: False
    IsNested: False
    IsNestedAssembly: False
    IsNestedFamANDAssem: False
    IsNestedFamORAssem: False
    IsNestedFamily: False
    IsNestedPrivate: False
    IsNestedPublic: False
    IsNotPublic: False
    IsPointer: False
    IsPrimitive: False
    IsPublic: True
    IsSealed: False
    IsSecurityCritical: False
    IsSecuritySafeCritical: False
    IsSecurityTransparent: True
    IsSerializable: False
    IsSpecialName: False
    IsUnicodeClass: False
    IsValueType: False
    IsVisible: True
    MemberType: TypeInfo {32}
    MetadataToken: 33554450
    [Module] (System.Reflection.MemberInfo): {MyExcelVSTO.dll}
    [Module]: {MyExcelVSTO.dll}
    Name: "Range"
    [Namespace]: "Microsoft.Office.Interop.Excel"
    ReflectedType: Nothing
    StructLayoutAttribute: Nothing
    TypeHandle: {System.RuntimeTypeHandle}
    TypeInitializer: Nothing
    UnderlyingSystemType: {Name = "Range" FullName = "Microsoft.Office.Interop.Excel.Range"}


?Globals.ThisAddIn.Application.Selection.GetType
{Name = "__ComObject" FullName = "System.__ComObject"}
    [Assembly]: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
    AssemblyQualifiedName: "System.__ComObject, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    Attributes: Public {1}
    BaseType: {Name = "MarshalByRefObject" FullName = "System.MarshalByRefObject"}
    ContainsGenericParameters: False
    CustomAttributes: Count = 1
    DeclaredConstructors: {Length=1}
    DeclaredEvents: {Length=0}
    DeclaredFields: {Length=1}
    DeclaredMembers: {Length=11}
    DeclaredMethods: {Length=9}
    DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__23}
    DeclaredProperties: {Length=0}
    DeclaringMethod: 'DirectCast(Globals.ThisAddIn.Application.Selection.GetType, System.RuntimeType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
    DeclaringType: Nothing
    FullName: "System.__ComObject"
    GUID: {00000000-0000-0000-0000-000000000000}
    GenericParameterAttributes: 'DirectCast(Globals.ThisAddIn.Application.Selection.GetType, System.RuntimeType).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: 'DirectCast(Globals.ThisAddIn.Application.Selection.GetType, System.RuntimeType).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
    GenericTypeArguments: {Length=0}
    GenericTypeParameters: {Length=0}
    HasElementType: False
    ImplementedInterfaces: {Length=0}
    IsAbstract: False
    IsAnsiClass: True
    IsArray: False
    IsAutoClass: False
    IsAutoLayout: True
    IsByRef: False
    IsCOMObject: True
    IsClass: True
    IsConstructedGenericType: False
    IsContextful: False
    IsEnum: False
    IsExplicitLayout: False
    IsGenericParameter: False
    IsGenericType: False
    IsGenericTypeDefinition: False
    IsImport: False
    IsInterface: False
    IsLayoutSequential: False
    IsMarshalByRef: True
    IsNested: False
    IsNestedAssembly: False
    IsNestedFamANDAssem: False
    IsNestedFamORAssem: False
    IsNestedFamily: False
    IsNestedPrivate: False
    IsNestedPublic: False
    IsNotPublic: False
    IsPointer: False
    IsPrimitive: False
    IsPublic: True
    IsSealed: False
    IsSecurityCritical: False
    IsSecuritySafeCritical: False
    IsSecurityTransparent: True
    IsSerializable: False
    IsSpecialName: False
    IsUnicodeClass: False
    IsValueType: False
    IsVisible: False
    MemberType: TypeInfo {32}
    MetadataToken: 33554625
    [Module] (System.Reflection.MemberInfo): {CommonLanguageRuntimeLibrary}
    [Module]: {CommonLanguageRuntimeLibrary}
    Name: "__ComObject"
    [Namespace]: "System"
    ReflectedType: Nothing
    StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
    TypeHandle: {System.RuntimeTypeHandle}
    TypeInitializer: Nothing
    UnderlyingSystemType: {Name = "__ComObject" FullName = "System.__ComObject"}

2 个答案:

答案 0 :(得分:0)

首先检查您的引用。

当我同时包含对Microsoft.Office.Interop.ExcelMicrosoft office 15.0 object library的引用时,我可以看到问题。后来我只尝试了第一个。问题不见了。

如果引用没有问题,请按照以下说明尝试。

首先获取活动工作表的对象。

然后使用工作表对象检查选择内容

Dim wsSheet As Excel.Worksheet = CType(Globals.ThisWorkbook.ActiveSheet, Excel.Worksheet)    

If wsSheet.Cells.Application.Selection.GetType Is GetType(Excel.Range) Then

End If

答案 1 :(得分:0)

替代方法是创建一个IsRange函数并捕获任何system.InvalidCastException,这是VB尝试转换{{1}时Type Exception的{​​{1}} }变成thrown

例如:

Selection
Excel.Range