Word 宏错误消息

发布于 2024-07-06 20:31:02 字数 139 浏览 3 评论 0原文

我正在更改文档模板宏。 我不知道该怎么做的一件事是自定义错误消息。 例如,文档中的错误消息是

“错误!未找到图表条目”

我想更改此消息以显示其他内容。 是否可以使用 Word VBA 或 VBScript 来完成此操作?

I am changing document template macros. The one thing I can't find out how to do is to customize error messages. For example an error message in a document is

"Error! No table of figures entries found"

I would like to change this to display something else. Is it possible to do this with Word VBA or VBScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-07-13 20:31:02

是否可以将其放入某些
某种全局错误处理程序? ——克雷格

这是可能的。 这是一个非常粗略的例子。

在标准模块中:

Sub HandleErr(ErrNo As Long)
    Select Case ErrNo
    Case vbObjectError + 1024
        MsgBox "No table of figures entries found.", vbOKOnly + vbCritical

    Case vbObjectError + 1034 To vbObjectError + 4999
        MsgBox "Still no table of figures entries found.", vbOKOnly + vbCritical

    Case Else
        MsgBox "I give up.", vbOKOnly + vbCritical, _
            "Application Error"
    End Select
End Sub

一些代码:

Sub ShowError()
Dim i As Integer

On Error GoTo Proc_Err

    'VBA Error
    i = "a"

    'Custom error
    If Dir("C:\Docs\TableFigs.txt") = "" Then
        Err.Raise vbObjectError + 1024
    End If

Exit_Here:
    Exit Sub

Proc_Err:
    If Err.Number > vbObjectError And Err.Number < vbObjectError + 9999 Then
        HandleErr Err.Number
    Else
        MsgBox Err.Description
    End If
End Sub

Is it possible to put this in some
kind of global error handler? – Craig

It is possible. Here is a very rough example.

In a standard module:

Sub HandleErr(ErrNo As Long)
    Select Case ErrNo
    Case vbObjectError + 1024
        MsgBox "No table of figures entries found.", vbOKOnly + vbCritical

    Case vbObjectError + 1034 To vbObjectError + 4999
        MsgBox "Still no table of figures entries found.", vbOKOnly + vbCritical

    Case Else
        MsgBox "I give up.", vbOKOnly + vbCritical, _
            "Application Error"
    End Select
End Sub

Some code:

Sub ShowError()
Dim i As Integer

On Error GoTo Proc_Err

    'VBA Error
    i = "a"

    'Custom error
    If Dir("C:\Docs\TableFigs.txt") = "" Then
        Err.Raise vbObjectError + 1024
    End If

Exit_Here:
    Exit Sub

Proc_Err:
    If Err.Number > vbObjectError And Err.Number < vbObjectError + 9999 Then
        HandleErr Err.Number
    Else
        MsgBox Err.Description
    End If
End Sub
掀纱窥君容 2024-07-13 20:31:02

如果要在 VBA 中捕获特定错误类型,一种方法是使用 On Error Resume Next,然后在要捕获的操作后面的行上测试错误消息,例如:

On Error Resume Next
' try action
If Err.Number <> 0 Then
  ' handle w/ custom message
  Err.Clear
End If

如果您知道确切的错误号 (If Err.Number = N 那么),那当然更好。

If you want to trap a specific error type in VBA, one method is to use On Error Resume Next then test for an error message on the line following the action to trap, e.g.:

On Error Resume Next
' try action
If Err.Number <> 0 Then
  ' handle w/ custom message
  Err.Clear
End If

If you know the exact error number (If Err.Number = N Then), that would be better of course.

乱世争霸 2024-07-13 20:31:02

好吧,如果您正在谈论拥有一个自定义消息框 - 那很简单。
在 VBA 帮助中查找“msgbox”以获得更好的信息。

Msgbox("Error! No table of figures entries found",16,"Error")

16 使其成为一条“批评”消息。

如果您正在谈论错误捕获,那么您将需要这样的代码:

On Error Resume Next
n = 1 / 0    ' this causes an error
If Err.Number <> 0 Then 
    n = 1
    if Err.Number = 1 Then MsgBox Err.Description
End If

当抛出错误时,将向 Err 对象提供一个数字和描述。

Well if you are talking about having a custom message box - that's easy.
Look up 'msgbox' in VBA help for better info.

Msgbox("Error! No table of figures entries found",16,"Error")

The 16 makes it a 'criticial' message.

If you're talking about error trapping then you'll need code like this:

On Error Resume Next
n = 1 / 0    ' this causes an error
If Err.Number <> 0 Then 
    n = 1
    if Err.Number = 1 Then MsgBox Err.Description
End If

When an error is thrown, a number and description are given to the Err object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文