Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Error Trapping in VBA (Word 97)

    Hello Folk
    Can someone tell me where I have gone wrong in my error trapping philosophy? The following code catches a "divide by 0", error on the first pass, but if the error is repeated, the program execution is not passed to the "CatchError" label and a fatal error results.

    Many thanks in advance
    Don



    Sub TestForErrorCatching()
    '
    Dim a As Single
    Dim b As Single
    Dim c As Single

    a = 100

    ReInput:
    b = InputBox("Input")

    On Error GoTo CatchError
    c = a / b
    On Error GoTo 0
    c = 2 * c
    Exit Sub

    CatchError:
    On Error GoTo 0
    GoTo ReInput
    End Sub
    Regards
    Don

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

    Re: Error Trapping in VBA (Word 97)

    You must use <font face="Georgia">Resume ReInput</font face=georgia> instead of <font face="Georgia">GoTo ReInput</font face=georgia>. This clears the error. Also, I would remove the line <font face="Georgia">On Error Goto 0</font face=georgia> in the error handling section.

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Trapping in VBA (Word 97)

    In your error handler you have the line:

    On Error Goto 0

    That turns off the error handling for the procedure.

    You want the tail end of your procedure to be:

    <pre>CatchError:

    Resume ReInput

    End Sub</pre>


    That will "bounce" you back up to the ReInput line.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  4. #4
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Error Trapping in VBA (Word 97)

    Thank You Hans
    That little misunderstanding has been a thorn in my side for months.
    Don
    Regards
    Don

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Error Trapping in VBA (Word 97)

    Thank you Bryan
    Such timely solutions to one of my problems is a rare thing these days.
    Don
    Regards
    Don

Posting Permissions

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