I need to detect all textboxes in a Word document.
Basically, depending on my documents, I have several shapes of different types. Some of them are textboxes, others are of other types, which may also include textboxes inside. Still following me? ;-)
I have the following code, but the part that is causing problems is after the Else, and the error handler:
For Each Shp In Application.ActiveDocument.Shapes
If Shp.TextFrame.HasText Then
'Doing some actions
'This part is working well
Else 'Check if the shape contains one or more textboxes
On Error GoTo ShapeErrorHandler
If Shp.GroupItems.Count > 0 Then
Shp.Ungroup
End If
For i = 1 To Shp.CanvasItems.Count
ShapeErrorHandler:
Err.Clear
Exit For
Next i
End If
Next
On some shapes that are not textboxes, the code goes into the Else, and I get the error "this member can only be accessed for a group" (pointing to GroupItems.Count).
With the error handler, the first error is handled correctly, skips the shape and checks the next one. But for the next shape that goes into the "else" part again, the error is not flagged and the control returns directly to the calling procedure, meaning there was an error, that was not handled in the code.
Now, I guess there are other ways to check if the shape contains textboxes. I am pretty sure you can direct me to a better method.
Otherwise, I guess my error handler is not correctly coded.
Any suggestion?
Basically, depending on my documents, I have several shapes of different types. Some of them are textboxes, others are of other types, which may also include textboxes inside. Still following me? ;-)
I have the following code, but the part that is causing problems is after the Else, and the error handler:
For Each Shp In Application.ActiveDocument.Shapes
If Shp.TextFrame.HasText Then
'Doing some actions
'This part is working well
Else 'Check if the shape contains one or more textboxes
On Error GoTo ShapeErrorHandler
If Shp.GroupItems.Count > 0 Then
Shp.Ungroup
End If
For i = 1 To Shp.CanvasItems.Count
ShapeErrorHandler:
Err.Clear
Exit For
Next i
End If
Next
On some shapes that are not textboxes, the code goes into the Else, and I get the error "this member can only be accessed for a group" (pointing to GroupItems.Count).
With the error handler, the first error is handled correctly, skips the shape and checks the next one. But for the next shape that goes into the "else" part again, the error is not flagged and the control returns directly to the calling procedure, meaning there was an error, that was not handled in the code.
Now, I guess there are other ways to check if the shape contains textboxes. I am pretty sure you can direct me to a better method.
Otherwise, I guess my error handler is not correctly coded.
Any suggestion?