Greg said:
Thanks Gents,
I did reply last night, but see this morning that it didn't transmit.
I have had a few error messages the past two days using OE that
newsgroups coudn't be resolved?
Anyway, thanks for both replies. Jezebel, by working I just meant the
code would run without generating a runtime error. With your reply
and Jay's I see now why it would and the others wouldn't. It was a
poor example.
The basic problem was this. I wanted an error handler that when an
error was generated that it would simply ignore it an go on to save
the document. I was working with TestA below which worked OK except
when the user canceled or "X" out of the SaveAs Dialog. It would
then throw and error while in the error handler.
Sub TestA()
On Error GoTo Err_Handler
MsgBox ActiveDocument.Bookmarks(1).Range.Text
'Other Stuff
Exit Sub
Err_Handler:
ActiveDocument.Save
End Sub
I can work throug that issue using this construction. Do either of
you see problems with this method? Thanks.
Sub TestB()
On Error GoTo Err_Handler
MsgBox ActiveDocument.Bookmarks(1).Range.Text
'Other Stuff
Exit Sub
Err_ReEntry:
On Error Resume Next
ActiveDocument.Save
Exit Sub
Err_Handler:
Resume Err_ReEntry
End Sub
Your TestB will work, and that style is common. But I personally find it
nonlinear and unintuitive, sending execution down to the bottom of the code
only to make it jump back to the point of the error. Besides that, if there
are multiple places in the routine that could trigger several different
kinds of errors, they all wind up going through Err_Handler and back to
wherever the error was. It can be a nightmare to debug, especially with
data-dependent errors.
I prefer a more linear and localized style, like this:
Sub TestJ()
On Error Resume Next
MsgBox ActiveDocument.Bookmarks(1).Range.Text
If Err.Number > 0 Then ' or just If Err.Number Then
Err.Clear
ActiveDocument.Save
Exit Sub
End If
'Other Stuff
End Sub
Each statement that could trigger an error can have a separate "error
handler" within an If Err.Number .. End If clause that immediately follows
that statement. You can nest these If clauses, one within another. If one
statement could trigger several kinds of errors, you can replace the simple
If clause with an If Err.Number = X .. ElseIf Err.Number = Y .. End If or a
Select Case Err.Number statement to do different things for different
errors. [If you program in other languages, you may recognize this style as
VBA's dumbed-down version of Try .. Catch blocks.]
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.