views:

449

answers:

3

One of our clients has decided they need their document template macros changed and I am ~lucky~ enough to be given the job. The one thing I can't find out how to do is to customize error messages. For example an error message they get 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 macros or VBScript ?

A: 

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.

Nick Hebb
A: 

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.

Mrgreen
Is it possible to put this in some kind of global error handler?
Craig
+1  A: 

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
Remou