gpt4 book ai didi

excel - vbObjectError 应该使用哪些数字?

转载 作者:行者123 更新时间:2023-12-04 19:48:56 27 4
gpt4 key购买 nike

This page 说:

"If you are generating errors, then you should add your number to thevbObjectError constant. Eg Err.Raise Number = vbObjectError + 1000.The range 513 - 65535 is available for user defined errors."


还有 O'Reilly says :

"we are left with numbers in the range vbObjectError + 512 to vbObjectError + 65535"


好的,我做到了,但是我得到了一个本地错误,而不是自定义错误:
Err.Raise vbObjectError + 515
Automation error
A syntax error occurred trying to evaluate a query string
This site 表示该错误消息的错误代码是
EVENT_E_QUERYSYNTAX As Long = &H80040203
VBA 即时 Pane 证实了这一点:
?&H80040203
-2147220989 <-- equals vbObjectError + 515
bettersolutions.com 还说:

"The range 513 - 65535 is available for user defined errors"


如果我应该添加 vbObjectError,并且如果 513-65535 可用,那么为什么 Err.Raise vbObjectError + 515 会给我一个 native Visual Basic 错误!?
好的,假设他们的意思是我的自定义代码应该从 513 开始,忘记 vbObjectError。但是 bettersolutions Codes page 显示了 520、521、一些 700 和一些 31000 的 native 错误。
我已经阅读了关于这个主题的各种文章和 SO 页面,但仍然非常困惑。
我读过的微软文档似乎给出了不同的建议:

"User-defined error numbers should be greater than this value."https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.constants.vbobjecterror


那么,我可以从 vbObjectError + 1 开始吗?
更新 1: native 错误
此过程列出您设置的任何数字范围内的每个唯一错误。
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 万到 +100 万的每个数字以及 -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 作为歧义符的可能解决方案。

最佳答案

If i'm supposed to add vbObjectError, and if 513-65535 is usable, then why does Err.Raise vbObjectError + 515 give me a native VB error!?


它没有。这不是原生 VB 错误。 Native VB errors没有 vbObjectError在他们中。
VBA 是建立在 COM 之上的,并且都是关于 COM 对象的。 vbObjectError , 为 0x80040000 , 代表 HRESULT error code具有:
  • S = 1 ( SEVERITY_ERROR ),表示失败,
  • Facility = FACILITY_ITF

  • 关于 FACILITY_ITF该文件指出:

    For most status codes returned from interface methods. The actual meaning of the error is defined by the interface. That is, two HRESULTs with exactly the same 32-bit value returned from two different interfaces might have different meanings.


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

    So, i can start at 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 可用于您的自定义错误,而 Trappable errors声称代码 0-1000 保留供将来使用(并列出 30000 中也被占用的一些代码)。请注意,这是关于不涉及添加任何 vbObjectError 的原始数字。 s - 这可能是误解 vbObjectError + 513 的根源。是最小的可用数字。
    或者,您可能会说这不是一种误解,而是一个经验法则,以使您的错误代码保持同步: 513从模块中提出时, vbObjectError + 513从物体上抬起时。
    我个人经常发现 Err.Raise 5, , "Description"非常适合大多数用途。

    关于excel - vbObjectError 应该使用哪些数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69370099/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com