Why would VBA TypeOf operator fail

后端 未结 3 1677
走了就别回头了
走了就别回头了 2021-01-08 00:34

I have been fighting with an Excel 2007 problem for several days now. Below is a listing of all facts I can think of that might be relevant:

  1. IDetailShee

3条回答
  •  情深已故
    2021-01-08 01:07

    There are a few ways you could cheat using CallByName. You're going to have to work around this bug one way or another.

    A quick dirty example

    Every sheet that starts with an implementing line should have a public GetType function. I attached the "TestSheet" sub to a button on my ribbon. It puts the returned type name in cell A1 to demonstrate the function.

    Module1

    '--- Start Module1 ---
    Option Explicit
    
    Public Sub TestSheet()
      Dim obj As Object
      Set obj = ActiveSheet
      ActiveSheet.[A1] = GetType(obj)
    End Sub
    
    Public Function GetType(obj As Object) As String
      Dim returnValue As String
      returnValue = TypeName(obj)
      On Error Resume Next
      returnValue = CallByName(obj, "GetType", VbMethod)
      Err.Clear
      On Error GoTo 0
      GetType = returnValue
    End Function
    '--- End Module1 ---
    

    Sheet1

    '--- Start Sheet1 ---
    Implements Class1
    Option Explicit
    
    Public Function Class1_TestFunction()
    End Function
    
    Public Function GetType() As String
        GetType = "Class1"
    End Function
    '--- End Sheet1 ---
    

提交回复
热议问题