Share via

Repeated VBA errors not trappable?

Anonymous
2011-06-28T00:55:59+00:00

I have a document that contains a table with merged table cells. I want to insert text into it using a VBA macro, but, failing that, I want the macro to continue through the document.

I have a loop as follows in my code:

    Selection.Find.Execute

    While Selection.Find.Found

        If Len(Trim(Selection.Text)) > 0 Then

            With Selection.Range

                On Error GoTo annotateSingleUnderlineErrorBypass

                .InsertBefore " [begin addition] "

                .InsertAfter " [end addition] "

annotateSingleUnderlineErrorBypass:

                On Error GoTo 0

            End With

            Selection.MoveRight Unit:=wdCharacter, Count:=1

            With Selection.Find

                .ClearFormatting

                .Text = " [end addition] "

                .Execute

                .Text = ""

        .Font.Underline = wdUnderlineSingle

            End With

            Selection.MoveRight Unit:=wdCharacter, Count:=1

        Else

            Selection.MoveRight Unit:=wdCharacter, Count:=1

        End If

        Selection.Find.Execute

    Wend

Earlier in the document, the error routine successfully bypasses error 5251 "This is not a valid action for the end of a row". But when the macro hits the merged cell, it is as if my error trapping is ignored.

Clicking the debug button gives the active command as being .InsertBefore " [begin addition] "

Why is error trapping successful on error 5251 but not on error 5960? How can I trap the 5960 error and have the macro continue on it's merry way?

Follow-up: I suspect the problem is actually that I cannot trap repeated errors. I copied the above trap code into another macro that marks strikethroughs:

While Selection.Find.Found

        If Len(Trim(Selection.Text)) > 0 Then

            With Selection.Range

                On Error GoTo annotateStrikethroughErrorBypass

                .InsertBefore " [begin deletion] "

                .InsertAfter " [end deletion] "

annotateStrikethroughErrorBypass:

                On Error GoTo 0

            End With

            Selection.MoveRight Unit:=wdCharacter, Count:=1

            With Selection.Find

                .ClearFormatting

                .Text = " [end deletion] "

                .Execute

                .Text = ""

                .Font.StrikeThrough = True

            End With

            Selection.MoveRight Unit:=wdCharacter, Count:=1

        Else

            Selection.MoveRight Unit:=wdCharacter, Count:=1

        End If

        Selection.Find.Execute

    Wend

Without the error-handling code it gets the 5251 error at the end of the first row.

With the error-handling code it gets the 5251 error at the end of the second row.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2011-06-28T10:15:13+00:00

Why is error trapping successful on error 5251 but not on error 5960? How can I trap the 5960 error and have the macro continue on it's merry way?

It has nothing to do with the error itself, the problem is that the error handling did not work in loops the way you did. (I don't know why.)

Install the errorhandler "On Error Goto ..." only at the beginning at the sub and handle the error's by a "Resume" / "Resume Next" in the errorhandler part.

Or use "On Error Resume Next"  at the beginning and use "if Err.Number <> 0 then ... Err.Clear" in your loop.

Andreas.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-06-28T17:55:58+00:00

    Err.Clear did the trick!

    Was this answer helpful?

    0 comments No comments