Errro Handling

G

Greg

Can someone please explain the workings of each of the three little
macros below? In each test the varialbe "test" does not exist so an
error is expected. Thanks


Sub Test1()
Dim i As Integer
For i = 1 To 3
On Error GoTo Clear
ActiveDocument.Variables("Test").Delete
Clear:
Err.Clear
MsgBox i
Next
End Sub
Sub Test2()
Dim i As Integer
For i = 1 To 3
On Error GoTo Clear
ActiveDocument.Variables("Test").Delete
Clear:
On Error GoTo 0
MsgBox i
Next
End Sub
Sub Test3()
Dim i As Integer
For i = 1 To 3
On Error GoTo Clear
ActiveDocument.Variables("Test").Delete
Clear:
On Error GoTo -1
MsgBox i
Next
End Sub
 
T

Tom Winter

Uh...what are you trying to do here? Are you actually trying to work with
document variables, or are you just doing this to understand error handling?

Err.Clear just sets Err.Number, Err.Description, etc to zero or an empty
string. It doesn't change your "error handling" or what your error handler
is.

On Error Goto 0 makes it so you don't have any error handler. If an error
occurs after that, it's as though you never set an error handler

I've never seen On Error Goto -1, so I would avoid it. I imagine it's like
On Error Goto 0. I imagine it's some weird construct that VBA allows just
for compatibility with some weird old version of basic.

Changing your error handler in the middle of a For Next loop is a really bad
idea. If you need to do something in a loop that might cause an error, call
a sub routine in the loop and have it do the meat of the job:

Sub Test()
For i = to 3
SubThatDoesSomething(i)
Next
End Sub

Sub SubThatDoesSomething(iWhich As Integer)
On Error ....

End Sub

You might want to check out On Error Resume Next. That basically causes your
code to ignore errors and keep going no matter what. You can then check if
Err.Number is non-zero after each statement (if you want) to see if an error
just occurred.
 
G

Greg Maxey

Tom,

Yeah, the question is poorly worded. I am using a non-existent varialbe to
purposely generate an error with Each loop and trying understand the use of
Error Handlers and errors in general a little better.

While I have since abandoned the below approach, I was trying to use Error
Handling to branch to aplicable areas of my code to rearrange the elements
in a list of names:

James Miller
Margaret E. Smith
John R. Richland Sr

I knew and error whould be generated if there where less than 3 elements and
less than 4 elements in each name above. The code worked for the line
above, but a run time error occured while processing the second line. I
thought that the Err.Clear line in my Contstruct segments would reset the
Error Handler so that it would process as I expected on subsequent loops.
It didn't.

Helmut Weber then suggested that I change Err.Clear to On Error Goto -1. As
you can see in the little samples I posted earlier. That method will work.

So basically I am just trying to figure out what is On Error Goto -1 and why
Err.Clear doesn't reset the handler like the help file says it does (or at
least it seems like that it says it does.) Thanks.

Sub Test
Dim oPara As Word.Paragraph
Dim oRng As Word.Range
Dim pRef1 As String
Dim pRef2 As String
Dim pRef3 As String
Dim pRef4 As String

For Each oPara In ActiveDocument.Paragraphs
Set oRng = oPara.Range
oRng.MoveEnd wdCharacter, -1
pRef1 = Split(oRng)(0)
pRef2 = Split(oRng)(1)
On Error GoTo Construct1
pRef3 = Split(oRng)(2)
On Error GoTo Construct2
pRef4 = Split(oRng)(3)
GoTo Construct3

Construct1:
Err.Clear
oRng.Text = pRef2 & ", " & pRef1
GoTo Finished
Construct2:
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2
GoTo Finished
Construct3:
If InStr("SRSrJRJrIIIV", pRef4) > 0 Then
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2 & ", " & pRef4
GoTo Finished
End If
Finished:
Next
End Sub
 
T

Tom Winter

Err.Clear does not reset the error handler and the help file does not say
that it does. The help file says "Use Clear to explicitly clear the Err
object". Remember, "Err" is an OBJECT with properties. The Clear method of
the Err object just changes the properties of the Err object. Its Number
property is set to zero, its Description property is set to an empty string,
etc. That's all the Clear method does. Don't expect it to do anything else.
If clearing out those properties doesn't help you somehow, then you don't
want to use Err.Clear for anything.

I believe one of the posters in this thread said that using error handling
to control the flow of your program is a bad idea. I heartily concur.
Perhaps you should check out how many elements there are in the array
returned by the split function:

Dim MyArray() As String

MyArray = Split(oRange.Text)

' We add one since split returns a ZERO based array. This will give us the
NUMBER of elements in the array.

Select Case UBound(MyArray) + 1

Case 1
Msgbox "There are one elements in the array"
Case 2
Msgbox "There are two elements in the array"

etc.....

Case Else
MsgBox "Oops, I can't handle this many elements!"

End Select
 
G

Greg

Tom,

Thanks for the polite schooling. Yes, I have abandoned using Errors to
direct branching and opted for a Select Case method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top