Results 1 to 3 of 3

Thread: check if shape is a textbox or contains textboxes

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    71
    Location

    check if shape is a textbox or contains textboxes

    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?

  2. #2
    VBAX Regular
    Joined
    Nov 2011
    Posts
    71
    Location
    Sorry, here is the same code with clearer layout:

    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

  3. #3
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Cross-posted (and solved) at: http://www.tek-tips.com/viewthread.cfm?qid=1665903

    For cross-posting etiquette, please read: http://www.excelguru.ca/node/7
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •