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.