Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check if a shape is a textbox or contains textboxes

Status
Not open for further replies.

engdernw

Programmer
Nov 4, 2011
9
CA
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?
 
Why not simply use On Error Resume Next ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Why not? Probably because I am stupid and didn't think of it! ;-)
I just ran the code with your suggestion, and it seems to be working! So I just hope the part that checks if the shape contains other textboxes is correct... I will probably see it over time!
Thanks PHV!
 
I am the one who posted on vbaexpress... Both are very useful. I never thought those 2 forums were linked to each other! I actually found tips on one that are not on the other forum.
 
Instead of trapping for an error, I would check the shape's type to see if it is a msoGroup, and only access the groupitems if so.
Code:
For Each shp In Application.ActiveDocument.Shapes
If shp.TextFrame.HasText Then
'Doing some actions
'This part is working well
ElseIf shp.Type = msoGroup Then '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
 
Thanks MarkWalsh! I will definitely try this!
 
I am the one who posted on vbaexpress... Both are very useful. I never thought those 2 forums were linked to each other! I actually found tips on one that are not on the other forum.
The forums aren't linked but it is common courtesy to let people at each site know when you're asking the same question on multiple sites, so that people don't waste their time reinventing the wheel.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top