C# 验证所选内容是否在范围内

C# 验证所选内容是否在范围内,c#,excel,vb.net,vsto,C#,Excel,Vb.net,Vsto,在Excel(以及可能的其他Office应用程序)中,Globals.ThisAddIn.Application.Selection将具有不同的类型,具体取决于用户选择的内容:单元格、图表等 如何验证用户选择的单元格 如果Globals.ThisAddIn.Application.Selection.GetType是GetType(Excel.Range),则不起作用。问题似乎在于GetType(Excel.Range),它返回: 错误BC30560:命名空间中的“范围”不明确 “Microso

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

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

如果Globals.ThisAddIn.Application.Selection.GetType是GetType(Excel.Range),则
不起作用。问题似乎在于
GetType(Excel.Range)
,它返回:

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

有什么想法吗


更新 源代码已更改为以下内容,以避免名称空间不明确(如果有人理解此处的名称空间冲突…?)

但是当选择单元格时,
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"}
?GetType(global.Microsoft.Office.Interop.Excel.Range)
{Name=“Range”FullName=“Microsoft.Office.Interop.Excel.Range”}
[程序集]:{MyExcelVSTO,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null}
AssemblyQualifiedName:“Microsoft.Office.Interop.Excel.Range,MyExcelVSTO,版本=1.0.0.0,区域性=中性,PublicKeyToken=null”
属性:Public或ClassSemanticsMask或Abstract或Import{4257}
基本类型:无
ContainsGenericParameters:False
自定义属性:计数=6
declaredconstructor:{Length=0}
DeclaredEvents:{Length=0}
DeclaredFields:{Length=0}
DeclaredMembers:{Length=12}
DeclaredMethods:{Length=7}
DeclaredNestedTypes:{System.Reflection.TypeInfo.d_u23}
DeclaredProperties:{Length=5}
DeclaringMethod:'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range),System.RuntimeType)。DeclaringMethod'引发了类型为'System.InvalidOperationException'的异常
去极化类型:无
全名:“Microsoft.Office.Interop.Excel.Range”
GUID:{00020846-0000-0000-c000-0000000000 46}
GenericParameterAttributes:'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range),System.RuntimeType)。GenericParameterAttributes'引发了类型为'System.InvalidOperationException'的异常
GenericParameterPosition:'DirectCast(GetType(global.Microsoft.Office.Interop.Excel.Range),System.RuntimeType)。GenericParameterPosition'引发了类型为'System.InvalidOperationException'的异常
GenericTypeArguments:{Length=0}
GenericTypeParameters:{Length=0}
HasElementType:False
实现的接口:{Length=1}
伊莎贝拉特:是的
伊桑斯:没错
艾萨瑞:错
IsAutoClass:错误
IsAutoLayout:对
IsByRef:False
iscom对象:False
IsClass:错误
IsConstructedGenericType:False
IsContextful:False
伊塞努姆:错
IsExplicitLayout:错误
IsGenericParameter:False
IsGenericType:False
IsGenericTypeDefinition:False
IsImport:对
i接口:正确
IsLayoutSequential:错误
伊斯马沙尔比雷夫:错
IsNested:错误
IsNestedAssembly:False
IsNestedFamANDAssem:错误
IsNestedFamORAssem:错误
IsNestedFamily:False
IsNestedPrivate:False
IsNestedPublic:False
IsNotPublic:False
IsPointer:错
IsPrimitive:假
是的
IsSealed:错
IsSecurityCritical:错误
IsSecuritySafitical:错误
IsSecurityTransparent:正确
可序列化:False
IsSpecialName:False
IsUnicode类:False
IsValueType:False
可见:正确
MemberType:TypeInfo{32}
MetadataToken:33554450
[Module](System.Reflection.MemberInfo):{MyExcelVSTO.dll}
[模块]:{MyExcelVSTO.dll}
名称:“范围”
[命名空间]:“Microsoft.Office.Interop.Excel”
反射类型:无
StructLayoutAttribute:没有
TypeHandle:{System.RuntimeTypeHandle}
类型初始值设定项:无
underlineSystemType:{Name=“Range”FullName=“Microsoft.Office.Interop.Excel.Range”}
?Globals.ThisAddIn.Application.Selection.GetType
{Name=“\uuuucomObject”FullName=“系统.\uuucomObject”}
[程序集]:{mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089}
AssemblyQualifiedName:“系统.u-ComObject,mscorlib,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”
属性:公共{1}
BaseType:{Name=“MarshalByRefObject”FullName=“System.MarshalByRefObject”}
ContainsGenericParameters:False
CustomAttributes:计数=1
declaredconstructor:{Length=1}
DeclaredEvents:{Length=0}
DeclaredFields:{Length=1}
DeclaredMembers:{Length=11}
DeclaredMethods:{Length=9}
DeclaredNestedTypes:{System.Reflection.TypeInfo.d_u23}
DeclaredProperties:{Length=0}
DeclaringMethod:'DirectCast(Globals.ThisAddIn.Application.Selection.GetType,System.RuntimeType)。DeclaringMethod'引发了类型为'System.InvalidOperationException'的异常
去极化类型:无
全名:“系统对象”
GUID:{00000000-0000-0000-0000-000000000000}
GenericParameterAttributes:'DirectCast(Globals.ThisAddIn.Application.Selection.GetType,System.RuntimeType)。GenericParameterAttributes'引发了类型为'System.InvalidOperationException'的异常
GenericParameterPosition:'DirectCast(Globals.ThisAddIn.Application.Selection.GetType,System.RuntimeType)。GenericParameterPosition'引发了类型为'System.InvalidOperationException'的异常
GenericTypeArguments:{Length=0}
GenericTypeParameters:{Length=0}
HasElementType:False
实现的接口:{Length=0}
伊莎贝拉特:错
伊桑斯:没错
艾萨瑞:错
IsAutoClass:错误
IsAutoLayout:对
IsByRef:False
IsCOMObject:对
是的
IsConstructedGenericType:False
IsContextful:False
伊塞努姆:错
IsExplicitLayout:错误
IsGenericParameter:False
IsGenericType:False
IsGenericTypeDefinition:False
IsImport:错误
i接口:False
IsLayoutSequential:错误
伊斯马沙尔比雷夫:是的
?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"}
Dim wsSheet As Excel.Worksheet = CType(Globals.ThisWorkbook.ActiveSheet, Excel.Worksheet)    

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

End If