【问题标题】:Stuck with some weird error in VBA在 VBA 中遇到一些奇怪的错误
【发布时间】:2012-02-20 10:00:30
【问题描述】:

我正在做 powerpoint 2007 自动化项目。我正在使用宏编程(VBA)。运行宏时出现以下错误。

Err.Number= -2147024809 (80070057)

但我关心的不是错误因为我想捕捉这个错误并基于这个错误我想执行一些操作。

这就是为什么我尝试像这样编写错误处理代码的原因:

OnError Goto Err:
'some code

Err:
If Err.number = -2147024809 (80070057) then
'do something
end if
Resume next

所以基本上,如果我以这种方式写错误号,那么它就不允许了。它给出了错误。

主要的事情是当错误发生时它没有转到“Err:”。它只是弹出带有“结束”和“调试”选项的错误。

【问题讨论】:

  • 是的,但正如我上面提到的,我想捕捉这个错误。但它没有去 Err:我的 if 条件所在的位置。

标签: vba powerpoint powerpoint-2007


【解决方案1】:

虽然它似乎有效,但一方面我会谨慎使用保留对象名称 (Err) 作为标签。

On Error GoTo ErrorHandler

' Your code here

' Make sure you don't hit the errorhandler when there's
' no error:
NormalExit:
Exit Sub ' or Function, whichever this is

ErrorHandler:
If err.Number = 123456788 Then
    ' Take corrective action
    ' then continue
    Resume Next
End If
' Trap other specific or general errors here

' Then make sure you know where you're going to exit:
Resume NormalExit

如果您需要捕获可能仅在代码中某些位置发生的非常具体的错误,您还可以执行本地错误处理程序:

On Error Resume Next

' Some code that might cause problems

' Did it throw the error you're trying to trap?
If Err.Number = 12398745 then 
  ' Corrective action
End If

' And now we return to our regularly scheduled error trapping
On Error GoTo ErrorHandler

【讨论】:

    【解决方案2】:

    错误编号是数字:-2147024809,它只是显示为字符串"-2147024809 (80070057)" 为清楚起见(80070057) 是十六进制的错误编号。

    你想要的;

    if err.number = -2147024809 then ....
    

    如果你愿意的话

    if err.number = &h80070057 then ....
    

    【讨论】:

    • 是的,但正如我上面提到的,我想捕捉这个错误。但它没有去 Err:我的 if 条件所在的位置。它只是弹出带有“结束”和“调试”选项的错误。
    • 在 IDE tools->options->general & 确保只启用 break-on-unhandled-errors
    【解决方案3】:

    错误消息的80070057 部分是负数-2147024809 的无符号十六进制版本。删除那部分代码就可以了,如果您想跟踪数字的十六进制版本(可用于通过 Google 等研究错误),只需将其添加为注释即可。

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多