vbObjectError应该使用哪些数字



此页面显示:

"如果您正在生成错误,则应将您的号码添加到vbObjectError常量。例如Err.Raise Number=vbObjectError+1000。513-65535的范围可用于用户定义的错误">

O'Reilly还说:

"我们留下的数字在vbObjectError+512到vbObjectError+65535"的范围内;

好的,我做了,但我得到了一个本地的,而不是自定义的,错误:

Err.Raise vbObjectError + 515
Automation error
A syntax error occurred trying to evaluate a query string

这个网站说错误信息的错误代码是

EVENT_E_QUERYSYNTAX As Long = &H80040203

VBA即时窗格确认:

?&H80040203
-2147220989  <-- equals vbObjectError + 515

bettersolutions.com还表示:

"513-65535的范围可用于用户定义的错误";

如果我应该添加vbObjectError,并且513-65535是可用的,那么为什么Err.Raise vbObjectError + 515会给我一个本机Visual Basic错误!?

好吧,假设他们的意思是我的自定义代码应该从513开始,忘记vbObjectError。但bettersolutions Codes页面显示了520、521、700和31000的本地错误。

我读过关于这个主题的各种文章和SO页面,但仍然完全困惑。

我读过的微软文档似乎给出了不同的建议:

"用户定义的错误号应大于此值"https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.constants.vbobjecterror

那么,我可以从vbObjectError+1开始吗?

更新1:本机错误

此过程列出您设置的任何数字范围内的每个唯一错误。

Sub ListErrs()
Dim lRow As Long, lCol As Long, lErr As Long, sError As String
lCol = 4
Application.ScreenUpdating = False
For lErr = -2000000 To 1000000
Application.StatusBar = lErr
On Error Resume Next
Err.Raise lErr
If (Error <> sError) Then
lRow = lRow + 1
If lRow > 1048576 Then
lRow = 1
lCol = lCol + 1
End If
sError = Error
Cells(lRow, lCol) = lErr & " : " & sError
Windows(1).ScrollRow = lRow
End If
On Error GoTo 0
DoEvents
Next lErr

Application.ScreenUpdating = True
End Sub

我对从-200万到+10万的每一个数字,以及在-20亿(vbObjectErr)到-200万之间的许多(不是所有)数字进行了计算。我没有计算所有在-20亿(vbObjectErr)到-200万之间的数字,因为这需要一周的时间(也许有人可以更快地计算)。

这是我的发现。这些结果似乎与关于更好解决方案的信息不一致。

-2147221504 Automation error
-2147221230 ActiveX component can't create object
-2147221229 Automation error
-2147221164 ActiveX component can't create object
-2147221163 Automation error
-2147221021 ActiveX component can't create object
-2147221020 Automation error
-2147221018 File name or class name not found during Automation operation
-2147221017 Automation error
-2147221014 File name or class name not found during Automation operation
-2147221013 Automation error
-2147221006 Automation error
-2147221005 ActiveX component can't create object
-2147221004 Automation error
-2147221003 ActiveX component can't create object
-2147221002 Automation error
-2147220994 ActiveX component can't create object
-2147220993 Automation error
-2000000    Automation error
0   Invalid procedure call or argument
1   Application-defined or object-defined error
3   Return without GoSub
4   Application-defined or object-defined error
5   Invalid procedure call or argument
6   Overflow
7   Out of memory
8   Application-defined or object-defined error
9   Subscript out of range
10  This array is fixed or temporarily locked
11  Division by zero
12  Application-defined or object-defined error
13  Type mismatch
14  Out of string space
15  Application-defined or object-defined error
16  Expression too complex
17  Can't perform requested operation
18  User interrupt occurred
19  Application-defined or object-defined error
20  Resume without error
21  Application-defined or object-defined error
28  Out of stack space
29  Application-defined or object-defined error
35  Sub or Function not defined
36  Application-defined or object-defined error
47  Too many DLL application clients
48  Error in loading DLL
49  Bad DLL calling convention
50  Application-defined or object-defined error
51  Internal error
52  Bad file name or number
53  File not found
54  Bad file mode
55  File already open
56  Application-defined or object-defined error
57  Device I/O error
58  File already exists
59  Bad record length
60  Application-defined or object-defined error
61  Disk full
62  Input past end of file
63  Bad record number
64  Application-defined or object-defined error
67  Too many files
68  Device unavailable
69  Application-defined or object-defined error
70  Permission denied
71  Disk not ready
72  Application-defined or object-defined error
74  Can't rename with different drive
75  Path/File access error
76  Path not found
77  Application-defined or object-defined error
91  Object variable or With block variable not set
92  For loop not initialized
93  Invalid pattern string
94  Invalid use of Null
95  Application-defined or object-defined error
96  Unable to sink events of object because the object is already firing events to the maximum number of event receivers that it supports
97  Can not call friend function on object which is not an instance of defining class
98  A property or method call cannot include a reference to a private object, either as an argument or as a return value
99  Application-defined or object-defined error
321 Invalid file format
322 Can't create necessary temporary file
323 Application-defined or object-defined error
325 Invalid format in resource file
326 Application-defined or object-defined error
380 Invalid property value
381 Invalid property array index
382 Set not supported at runtime
383 Set not supported (read-only property)
384 Application-defined or object-defined error
385 Need property array index
386 Application-defined or object-defined error
387 Set not permitted
388 Application-defined or object-defined error
393 Get not supported at runtime
394 Get not supported (write-only property)
395 Application-defined or object-defined error
422 Property not found
423 Property or method not found
424 Object required
425 Application-defined or object-defined error
429 ActiveX component can't create object
430 Class does not support Automation or does not support expected interface
431 Application-defined or object-defined error
432 File name or class name not found during Automation operation
433 Application-defined or object-defined error
438 Object doesn't support this property or method
439 Application-defined or object-defined error
440 Automation error
441 Application-defined or object-defined error
442 Connection to type library or object library for remote process has been lost. Press OK for dialog to remove reference.
443 Automation object does not have a default value
444 Application-defined or object-defined error
445 Object doesn't support this action
446 Object doesn't support named arguments
447 Object doesn't support current locale setting
448 Named argument not found
449 Argument not optional
450 Wrong number of arguments or invalid property assignment
451 Property let procedure not defined and property get procedure did not return an object
452 Invalid ordinal
453 Specified DLL function not found
454 Code resource not found
455 Code resource lock error
456 Application-defined or object-defined error
457 This key is already associated with an element of this collection
458 Variable uses an Automation type not supported in Visual Basic
459 Object or class does not support the set of events
460 Invalid clipboard format
461 Method or data member not found
462 The remote server machine does not exist or is unavailable
463 Class not registered on local machine
464 Application-defined or object-defined error
481 Invalid picture
482 Printer error
483 Application-defined or object-defined error
735 Can't save file to TEMP
736 Application-defined or object-defined error
744 Search text not found
745 Application-defined or object-defined error
746 Replacements too long
747 Application-defined or object-defined error
65536   Invalid procedure call or argument

更新2:.Source

如果VB允许每个应用程序用消歧器定义自己的错误号(甚至是重叠的错误号),那似乎会更好,但我知道什么?我下面的答案演示了使用.Source作为diambiguator的可能解决方案。

如果我应该添加vbObjectError,并且513-65535是可用的,那么为什么Err.Raise vbObjectError+515会给我一个本地VB错误!?

没有。这不是本机VB错误。本机VB错误中没有vbObjectError

VBA是建立在COM之上的,它完全是关于COM对象的
vbObjectError,即0x80040000,表示具有以下的HRESULT错误代码

  • S = 1(SEVERITY_ERROR),表示故障,以及
  • Facility = FACILITY_ITF

关于FACILITY_ITF,文档说明:

对于从接口方法返回的大多数状态代码。错误的实际含义由接口定义。也就是说,从两个不同的接口返回的两个具有完全相同的32位值的HRESULT可能具有不同的含义。

这正是您在这里看到的。您合法地使用vbObjectError + 515的值作为自己的错误代码,但由于您从未为该自定义错误提供自定义描述,因此系统[un]为该HRESULT获取了一个默认COM描述,这是由其他COM组件定义的。

所以,我可以从vbObjectError+1开始?

您可以从vbObjectError+0开始。只需在Err.Raise的第三个自变量中提供文本描述:

Err.Raise vbObjectError + 515, , "This is totally my own error and not a syntax error about evaluating some strings."

话虽如此,请注意,只有当您从类模块中引发错误时,才将vbObjectError添加到实际错误代码中(坦率地说,只要您留在VBA中,不遵守此要求不会对您造成任何伤害)。当在模块中从普通函数中引发错误时,您甚至不会被要求这样做,而可以直接使用错误代码。

现在,有一个矛盾:Err.Raise指示您错误代码513–65535可用于自定义错误,而Trappible错误则声称代码0-1000保留供将来使用(并列出30000中也被占用的一些代码)。请注意,这是关于不涉及添加任何vbObjectError的原始数字——这可能是误解vbObjectError + 513是最低可用数字的原因
或者,您可能会说,这不是误解,而是保持错误代码同步的经验法则:513从模块引发,vbObjectError + 513从对象引发。

我个人经常发现Err.Raise 5, , "Description"非常适合大多数用途。

您可以使用以下代码,自己找出实际没有错误的数字:

Sub Errs()
Dim i As Long

On Error GoTo myErrorHandler
For i = 1 To 1000 ' change loop control values to your needs 
Err.Raise vbObjectError + i
Next

myErrorHandler:
If Err.Description = "Automation error" Then
With Worksheets("MyErrorSheet") ' change "MyErrorSheet" to your actual "error log" sheet name
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2).Value = Array(i, Err.Description)
End With
End If
Resume Next

End Sub

可能有任意数量的工作簿生成自定义错误。试图绕过本机数字并相互绕过会造成复杂而脆弱的错误处理情况。

这是一个建议的解决方案,我认为它可能会解决这个问题。

这个解决方案不在乎我的自定义错误号是否与本机错误号重叠。它使我能够拥有自定义的错误编号策略,而不用担心与本机错误(或其他工作簿或加载项生成的错误)发生冲突

该解决方案利用Err的.Source属性。通常,.Source会加载导致错误的VBA工程的名称。如果您已命名VBA工程,则该自定义名称将加载到.Source.中

因此,我们可以很容易地使用.Source来消除错误数字的歧义。

工作表上名为";tblCustomErrors":

ErrNum  ErrDescription
11  User clicked pause
53  User cancelled the process
1   User had a chicken
2   Chicken ate user

VBA:

Enum CustomErrs
LaidEgg = 1
UserEaten = 2
Paused = 11
Cancelled = 53
End Enum

Sub Test_Handler()
On Error GoTo HANDLER
' Native errors
Debug.Print 1 / 0            ' 11, Division by zero
Err.Raise 11                 ' same
Kill "C:Doesn't-Exist.txt"         ' 53, File not found
' Custom errors 
Err.Raise CustomErrs.Paused, xlCustom    ' re-uses native err number
Err.Raise CustomErrs.Cancelled, xlCustom    ' re-uses native err number
Err.Raise CustomErrs.LaidEgg, xlCustom
Err.Raise CustomErrs.UserEaten, xlCustom

Exit Sub
HANDLER:
GlobalHandler
Resume Next
End Sub

Sub GlobalHandler()
Dim sDesc As String
With Err
If (.Source = xlCustom) Then
' get description from worksheet
sDesc = Application.VLookup(.Number, [tblCustomErrors], 2, False)
.Description = sDesc
End If 
MsgBox .Description
End With
End Sub

枚举

使用Enum而不是显式数字的一个优点是,您可以获得有意义的智能感知和自文档化代码。

Err.Raise LaidEgg, xlCustom

注意事项:不要使用0作为错误号,它将以"自动化错误"失败。

我相信这可能是怎么回事。来源是有意使用的。

vbObjectError

此解决方案可以与vbObjectError结合使用,以避免本机编号,同时仍然提供集中处理的便利,方法如下:

Enum CustomErrs
LaidEgg = vbObjectError
UserEaten
Paused
Cancelled
End Enum

编码描述

或者,可以通过这种方式将描述硬编码到VBA中,而不是将描述存储在图纸表中。这是对上述VBA的修改。

...
Sub GlobalHandler()
...
Select Case Err.Number
Case LaidEgg
MsgBox "User had a chicken."
' other custom actions
Case UserEaten
MsgBox "Chicken ate the user."
' other custom actions
Case Paused
MsgBox "User clicked pause."
' other custom actions
Case Cancelled
MsgBox "User cancelled the process."
' other custom actions
End Select
...

相关内容

  • 没有找到相关文章

最新更新