Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    On Error (WordXP/VBA)

    I've read <post#=406446>post 406446</post#> (mandelbrot: On Error Goto Hell) and still don't understand the mechanics of On Error.

    In particular, the example below, cribbed from the help files, doesn't perform as I expect. I've used the getattr function because, ultimately, I want to test drive letters.

    I expect the first GetAttr to succeed and retun '16', which it does.
    I expect the second GetAttr to return '16' - which it does if I have a floppy in the drive.
    But with the floppy removed, i see an error '5' - which is NOT trapped by my On Error statement.

    What have I misunderstood? I'd thought that this would be about the simplest, trouble-free demonstratioon of error trapping possible.

    The "On Error GoTo ErrorHandler" should enables error-trapping, shouldn't it? Redirecting at least the first error to the named routine.

    When the Gettr("a:") fails, I expect the named routine to be Activated.
    But Word/VBA throws up a standard error box, and apparently ignores my error-handling routine altogether.


    <pre>Sub test()
    On Error GoTo ErrorHandler ' Enable error-handling routine.
    '
    MsgBox GetAttr("c:") ' 16; drive C exists on most PCs.
    ' 16 if a floppy is in the drive; drive A exists on most PCs.
    ' Err=5 if the drive is there but has no floppy diskette.
    MsgBox GetAttr("a:")
    Exit Sub ' Exit Sub to avoid error handler.
    '
    ErrorHandler: ' Error-handling routine.
    Select Case Err.Number ' Evaluate error number.
    Case 5 ' No-floppy-Present error.
    MsgBox "Please insert a floppy diskette"
    Case Else
    ' Handle other situations here....
    MsgBox "Error trapped as " & Err.Number & Err.Description
    End Select
    Resume ' Resume execution at same line that caused the error.
    End Sub</pre>


  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error (WordXP/VBA)

    Maybe it's not me, maybe it's my installation?

    I just thought to run the help code AS IS, and I can't get it to trap the errors here either:
    <pre>Sub OnErrorStatementDemo()
    On Error GoTo ErrorHandler ' Enable error-handling routine.
    Open "TESTFILE" For Output As #1 ' Open file for output.
    Kill "TESTFILE" ' Attempt to delete open
    ' file.
    On Error GoTo 0 ' Turn off error trapping.
    On Error Resume Next ' Defer error trapping.
    ObjectRef = GetObject("MyWord.Basic") ' Try to start nonexistent
    ' object, then test for
    'Check for likely Automation errors.
    If Err.Number = 440 Or Err.Number = 432 Then
    ' Tell user what happened. Then clear the Err object.
    Msg = "There was an error attempting to open the Automation object!"
    MsgBox Msg, , "Deferred Error Test"
    Err.Clear ' Clear Err object fields
    End If
    Exit Sub ' Exit to avoid handler.
    ErrorHandler: ' Error-handling routine.
    Select Case Err.Number ' Evaluate error number.
    Case 55 ' "File already open" error.
    Close #1 ' Close open file.
    Case Else
    ' Handle other situations here...
    End Select
    Resume ' Resume execution at same line
    ' that caused the error.
    End Sub
    </pre>


  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: On Error (WordXP/VBA)

    > When the Gettr("a:") fails, I expect the named routine to be Activated.

    The only thing On Error GoTo <label> does is make the code jump to <label> if an error occurs. There is no named procedure involved.

    You can test what the code does by single-stepping through it: position the insertion point somewhere in the test procedure and press F8. On my system, there is a pause when the line

    MsgBox GetAttr("A:")

    is executed - the system tries to read the floppy. After a few seconds, the code jumps to the line after the ErrorHandler label:

    Select Case Err.Number

    The error value is 5, so the message box "Please insert a floppy diskette" is shown, after which Resume sends the code back to MsgBox GetAttr("A:"). In short, I'm stuck in an endless loop until I provide a diskette, or break out of the code.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error (WordXP/VBA)

    > After a few seconds, the code jumps to the line after the ErrorHandler label:

    Ah yes, but then, you see, you hadn't chosen (VBE) Tools, Options, General Break On All Errors, had you? (grin!)

    That'll make a difference!

    Please don't ask me why I'd turned that on; probably part of my mind's wandering around in its customary idle curiousity.

    ( I had wondered whether in choosing GetAttr I'd managed to select the one aspect of VBA that didn't conform to error-handling... )

    Thanks, Hans, for getting me to re-examine my installation here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •