如何以编程方式打破所有错误? [英] How to Programmatically Break on All Errors?

查看:94
本文介绍了如何以编程方式打破所有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是设置一个从该选项开始的标志:

What I want to do is set a flag to go from this option:

为此选项:

这样做的原因是我在整个代码中都有错误处理程序,并且在调试时希望能够看到错误所在.

The reason for this is that I have error handlers throughout my code and whilst I'm debugging I want to be able to see what the errors are.

将错误标志设置为关闭

On Error GoTo 0

将带我回到使用错误处理的最后一个地方,就像我正在研究的错误处理之前的两个或三个过程一样.

Will just take me back to the last place error handling was used, like two or three procedures before the one I'm working on.

这背后的原因是我有一个很大的代码库,并且我知道有几个部分不需要查看错误是什么,但是一旦进入处理的某个部分,我就要做(很多次调用) ),这时我要打开打破未处理的错误.

The reasoning behind this is that I have a large codebase and I know there are several sections that I do not need to see what the errors are but I do once I get to a certain part of the processing (many hundreds of calls deep), it is at this point I want to turn on Break on Unhandled Errors.

推荐答案

我知道这个问题很旧,但是为需要在Excel VBA中执行此操作的任何人添加了答案.我有一个为公司开发的外接程序,它使用以下技术:

I know this question is old, but adding an answer for anyone that needs to do this in Excel VBA. I have an add-in that I developed for my company that uses the following technique:

  1. 使用注册表项检查不必要的错误捕获选项(此示例中的对所有错误都中断")
  2. 根据需要更改注册表项
  3. 重新启动Excel(我将这一部分设为手动,但是您可以根据情况自动执行)

Dim objShell As Object
Dim strErrOption As String
Set objShell = CreateObject("WScript.Shell")
On Error Resume Next
strErrOption = objShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\VBA\7.1\Common\BreakOnAllErrors")
If strErrOption = 1 Or Err.Number <> 0 Then 'Break on All Errors Set or Reg Key DNE
    On Error GoTo 0
    objShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\VBA\7.1\Common\BreakOnAllErrors", 0
    MsgBox ("Please Restart Excel")
End If
On Error GoTo 0

注意:Excel仅在打开时读取注册表,而仅在关闭时写入.这就是为什么需要重新启动应用程序的原因.

Note: Excel only reads the registry on open and only writes on close. This is why restarting the app is required.

  • 0是未处理错误的中断
  • 1是所有错误中断
  • 2是班级模块中断

这篇关于如何以编程方式打破所有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆