+ Reply to Thread
Results 1 to 12 of 12

Find out if Shape has Text

Hybrid View

a1s2d3f4 Find out if Shape has Text 08-04-2009, 06:42 PM
JBeaucaire Re: Find out if Shape has Text 08-04-2009, 08:15 PM
StephenR Re: Find out if Shape has Text 08-05-2009, 03:07 AM
Krishnakumar Re: Find out if Shape has Text 08-05-2009, 03:42 AM
Andy Pope Re: Find out if Shape has Text 08-05-2009, 04:22 AM
Krishnakumar Re: Find out if Shape has Text 08-05-2009, 04:55 AM
a1s2d3f4 Re: Find out if Shape has Text 08-05-2009, 08:28 AM
romperstomper Re: Find out if Shape has Text 08-05-2009, 08:30 AM
StephenR Re: Find out if Shape has Text 08-05-2009, 08:45 AM
romperstomper Re: Find out if Shape has Text 08-05-2009, 08:48 AM
a1s2d3f4 Re: Find out if Shape has Text 08-06-2009, 09:10 AM
Andy Pope Re: Find out if Shape has Text 08-06-2009, 09:52 AM
  1. #1
    Registered User
    Join Date
    08-04-2009
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Find out if Shape has Text

    Have an .xls file with multiple sheets and many objects (Shapes) on of those sheets.
    The objects are all rectangles, some of which contain text frames, where I had entered manually some text.

    Also, each shape has a name.

    Now, I want my VBA Macro to take a specific shape (using its name), and find out if it has text frame in it.

    i thought the way to do it is

    Set oShp = ActiveSheet.Shapes(X)
    If oShp.HasTextFrame Then  
       Msgbox oShp & "has text frame"
    End If
    ,where X is the string that is the name of the shape I am testing.

    Every example I could find had that type of code above.
    But in my case I get "Object doesn't support this property or method" when I call this code.

    Also, scanning through Shapes Collection properties, I don't actually see "HasTextFrame" mentioned. Only "HasDiagram".

    Would anyone have any suggestions?

    Thanks,
    a1
    Last edited by Andy Pope; 08-06-2009 at 09:53 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find out if Shape has Text

    Everything I've read about that property indicates it is a PowerPoint function.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Find out if Shape has Text

    Not sure, but you could try using the AlternativeText property. It appears by default to be populated with any text in the shape, but I don't know whether this is a hard and fast rule.
    Sub x()
    
    Dim s As Shape
    
    For Each s In ActiveSheet.Shapes
        If s.AlternativeText <> "" Then MsgBox s.Name & " has text"
    Next s
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-19-2005
    Location
    Gurgaon,India
    MS-Off Ver
    2007,2010,2013
    Posts
    180

    Re: Find out if Shape has Text

    May be...

    Set oShp = ActiveSheet.Shapes(x)
    If oShp.TextFrame.Characters.Count Then
       MsgBox oShp.Name & "has text frame"
    End If
    Kris

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Find out if Shape has Text

    Looks like the .Characters approach will only work if the shape has had text edited within it, even if it now contains no text.

    So the best approach would be a function that use the character count method but also contains error trapping
    Cheers
    Andy
    www.andypope.info

  6. #6
    Forum Contributor
    Join Date
    02-19-2005
    Location
    Gurgaon,India
    MS-Off Ver
    2007,2010,2013
    Posts
    180

    Re: Find out if Shape has Text

    Thanks Andy for the input. So may be function like this..

    Function HASTEXTINSHAPE(shpObj) As Boolean
    HASTEXTINSHAPE = False
    On Error Resume Next
    HASTEXTINSHAPE = Len(shpObj.TextFrame.Characters.Count)
    On Error GoTo 0
    End Function
    would work

  7. #7
    Registered User
    Join Date
    08-04-2009
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Find out if Shape has Text

    Thanks to all the replies.

    I could get StephenR's to work.

    When i tried Krishnakumar's
    Function HASTEXTINSHAPE(shpObj) As Boolean
    HASTEXTINSHAPE = False
    On Error Resume Next
    HASTEXTINSHAPE = Len(shpObj.TextFrame.Characters.Count)
    On Error GoTo 0
    End Function
    I couldn't get it to work, but maybe I did something wrong.
    As a test, I stuck it into a separate Module in my View Code window.
    Then, I made a test Sub Macro2() to call it.
    i.e.
    Sub Macro2()
    
        x = "Rectangle 168"
        If HASTEXTINSHAPE(x) Then
            MsgBox x & " has text"
        Else
            MsgBox x & " has no text"
        End If
    
    End Sub
    
    
    Function HASTEXTINSHAPE(shpObj) As Boolean
        HASTEXTINSHAPE = False
        On Error Resume Next
        HASTEXTINSHAPE = Len(shpObj.TextFrame.Characters.Count)
        On Error GoTo 0
    End Function

    But when I tried running it, I always got "Rectangle 168 has no text", even if I explicitly entered some text there.

    Since VBA is far from being my second language, I am sure I am doing something wrong here.

    Thanks,
    a1

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: Find out if Shape has Text

    Change the code to:
    Sub Macro2()
    
        Set x = Activesheet.Shapes("Rectangle 168")
        If HASTEXTINSHAPE(x) Then
            MsgBox x.Name & " has text"
        Else
            MsgBox x.Name & " has no text"
        End If
    
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Find out if Shape has Text

    That doesn't appear to get round the deleted text point made by Andy.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: Find out if Shape has Text

    I've used:
    Function ShapeHasText(shp as Shape) As Boolean
        On Error Resume Next
        ShapeHasText = (Len(shp.TextFrame.Characters.Text) > 0)
    End Function
    before.

  11. #11
    Registered User
    Join Date
    08-04-2009
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Find out if Shape has Text

    Ok, just to report:
    thanks, Romperstomper, for pointing out that I was sending a STRING! to the HASTEXTINSHAPE. That was silly of me.
    So, I did change the code to this:

    
    Sub Macro2()
        Dim x As Shape
        
        Set x = ActiveSheet.Shapes("Rectangle 168")
        
        If HASTEXTINSHAPE(x) <> False Then
            MsgBox x.Name & " has text"
        Else
            MsgBox x.Name & " has no text"
        End If
        
    End Sub
    
    Function HASTEXTINSHAPE(shpObj As Object) As Boolean
        HASTEXTINSHAPE = False
        On Error Resume Next
        HASTEXTINSHAPE = Len(shpObj.TextFrame.Characters.Count)
        On Error GoTo 0
    End Function
    and everything worked correctly.

    If I make a fresh shape, and execute the Macro, it has no text, and so the HASTEXTINSHAPE returns "no text". If I Add Text to it, even without typing any characters in the box, it return "there is text".

    Obviously, if I have Alternative Text associated with that shape, this function is not affected.

    Interestingly enough, when I tried to declare shpObj "as Shape" inside HASTEXTINSHAPE, it didn't like it at all.

    This is the error box:

    ---------------------------
    Microsoft Visual Basic
    ---------------------------
    Compile error:

    Variable required - can't assign to this expression
    ---------------------------
    OK Help
    ---------------------------



    But when I declared it as "Object", it was happy.

    That's the only puzzling thing for me, since clearly it's a "Shape".



    I am going to mark this as SOLVED since I have two different solutions here that approach this issue from two different angles.
    EDIT: Hmm, can't see a way to mark it as "SOLVED". Should I worry about this?


    *If someone uses Alternative Text method, make sure that you really don't have anything in the Alternative Text box (Format Autoshape), or this is not going to work.



    Thanks,
    a1
    Last edited by a1s2d3f4; 08-06-2009 at 09:13 AM.

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Find out if Shape has Text

    The error when declaring as shape is due to the incorrect usage of Len/Count.
    Once the compiler knows what the object is it can object

    
    ' so either just use the count
        HASTEXTINSHAPE = shpObj.TextFrame.Characters.Count
    
    ' or the len of the text
        HASTEXTINSHAPE = Len(shpObj.TextFrame.Characters.text)
    I will mark the post as solved. There is a time limit for users.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1