【问题标题】:Error handling in class returns runtime error 440 "automation error"类中的错误处理返回运行时错误 440“自动化错误”
【发布时间】:2020-01-13 04:27:12
【问题描述】:

我有一个带有let INN 属性的类ClsSubject。该属性检查传入参数是否为数字字符串并且长度是否为 10 个字符。否则会抛出错误。

Property Let INN(val As String)
    If IsNumeric(val) And Len(val) = 10 Then
        PINN = val
    Else
        Dim errorMessage As String
        errorMessage = val & " is an Invalid value"
        Err.Raise vbObjectError + 513, "ClsSubject", errorMessage
    End If
End Property

当我尝试模拟错误时,我只会得到 ​​p>

运行时错误“440”:自动化错误。

我做错了什么?

【问题讨论】:

  • 如何模拟错误?当我这样做时,我得到了预期的错误invalid value
  • Usign CallByName sub,但我也在一个新文件中做了一个@Vityata的例子,它只是给了我不同的错误描述
  • 我投票关闭,因为 它正在寻求调试帮助,但需要更多信息。应更新问题以包含所需行为、特定问题或错误以及重现问题所需的最短代码。

标签: excel vba


【解决方案1】:

使用 OP 中的代码,以下工作。在一个模块中:

Sub TestMe()

    Dim x As New clsSubject
    x.INN = 3276132761#
    Debug.Print x.PINN

End Sub

还有这个在课堂上:

Public PINN As String

Property Let INN(val As String)
    If IsNumeric(val) And Len(val) = 10 Then
        PINN = val
    Else
        Dim errorMessage As String
        errorMessage = val & " is an Invalid value"

        Err.Raise vbObjectError + 513, "ClsSubject", errorMessage
    End If
End Property

为了使代码运行,请调用TestMe()

在一般情况下,类的字段应该是私有的,并且应该通过公共属性访问。例如。 private PINN as String,但这只是为了说明。

【讨论】:

  • 我收到Runtime error -2147220991 (80040201) Automated Error: An event was unable to invoke any of the subscribers
  • @Igor Cheglakov:如果你正确复制了代码,当输入像x.INN = "YYY" 这样的字符串时,会出现下一个错误:Run-time error '-2147220991 (80040201)': "YYY" is an Invalid value。就像它已在您的类属性中设置一样...
猜你喜欢
  • 2014-01-14
  • 1970-01-01
  • 2014-11-29
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
相关资源
最近更新 更多