1

Suppose I have a TextRange object, and I need to find the Shape that contains that TextRange.

Normally, I can use the Parent property of the TextRange to get the TextFrame that contains it, and then use the Parent property again to get the Shape.

However, if the text is within a table cell, the Parent property of the TextRange is Nothing. (I think this is a "feature" of PowerPoint 2010). EDIT: this is not true except when accessing the TextRange via Selection.TextRange.

Is there any other way I can identify the shape (which in this case would be the table cell)?

UPDATE: thanks to KazJaw, I have looked at this again, and it turns out I can navigate up the Parent chain unless the TextRange I'm starting from was obtained from Selection.TextRange. For my purposes, this is less of a problem.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • could you show wider aspect of your code and needs?? – Kazimierz Jawor Apr 30 '13 at 18:59
  • @KazJaw: I have a large amount of legacy code that relies on being able to navigate up the `Parent` hierarchy to reach the `Shape`. As one example (of many), I have situations where I want to get a `TextRange` for the entire text of the shape, starting from a `TextRange` that encompasses only part of the `Shape`. To do this, I go up to the `Shape` and then get the `TextRange` associated with that. But that's just one example - there are lots more. – Gary McGill Apr 30 '13 at 22:47
  • I was asking about `wider aspect` to think of workaround. Anyway, I made some tests and it seems that it is a bit problematic. However, having in mind any cell within the table and text there- `TextRange.Parent` results with `TextFrame` as expected, `TextRange.Parent.Parent` >> `Shape` as expected, `TextRange.Parent.Parent.Parent` >> `Slide` as unexpected. I can't encounter `Nothing` which you have. – Kazimierz Jawor May 01 '13 at 06:34
  • I would be delighted if `TextRange.Parent.Parent` yielded a `Shape`, but it does not - not for me, in PowerPoint 2010, anyway. It did in previous versions of PowerPoint though. – Gary McGill May 01 '13 at 08:32
  • But you're right about the `Parent` of a `TextRange` being a `TextFrame`. I'd missed out that extra level in my question - will update. – Gary McGill May 01 '13 at 08:34
  • `TextRange.Parent.Parent` yield a `Shape` for me in PP2010 but... it's not the shape you are looking for but the shape of cell. So, my tip for you is- check something like `TextRange.Parent.Parent.Id` or `TextRange.Parent.Parent.Type`. If that raises an error you are rather in `Cell.Shape` object which could mean that you are in table. Having limited information about your project it's the only idea I have. – Kazimierz Jawor May 01 '13 at 08:39
  • @KazJaw: thanks for your help. It seems I was wrong - the `Parent` object does indeed lead to a `Shape` in most cases. Where it apparently does not is if you start off from the `Selection` object (`ActiveWindow.Selection.TextRange.Parent`). In that case, it's `Nothing` - however if you do `ActiveWindow.Selection.ShapeRange(1).Table.Cell(1,1).Shape.TextFrame.TextRange.Parent` (etc.) then that works OK. So confusing! – Gary McGill May 01 '13 at 09:39
  • @KazJaw: if you create an answer saying it's only a problem with the Selection object, I'll accept it :-) – Gary McGill May 01 '13 at 09:45
  • 1
    I decided to add some more information which I've also found a bit confusing... Thanks! – Kazimierz Jawor May 01 '13 at 10:41

2 Answers2

1

Based on further discussion in comments below question it seems that the real problem refers to selection object. If one select any text within the table then some test made in Immediate have the following results:

? Typename(ActiveWindow.Selection.TextRange)
TextRange
? Typename(ActiveWindow.Selection.TextRange.Parent)
Nothing
? Typename(ActiveWindow.Selection.TextRange.Parent.Parent)
'>>Error

Additional information also for other programmers. The following I've found a bit confusing making some test to answer the question. (For simple presentation with one slide, one table in it and some cells filled with text)

Sub Test_To_SO()
    Dim SL As Slide
    Set SL = ActivePresentation.Slides(1)

    Debug.Print TypeName(SL.Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Parent)
        'result >> TextFrame
    Debug.Print TypeName(SL.Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Parent.Parent)
        'result >> Shape
    Debug.Print TypeName(SL.Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Parent.Parent.Parent)
        'result >> Slide !!

End Sub
Kazimierz Jawor
  • 18,861
  • 7
  • 35
  • 55
0

It's not so much that you can't get there via the selection, it's sometimes a matter of what you've selected. The object model's broken for text in table cells. As noted, ActiveWindow.Selection.TextRange.Parent returns nothing for selected table cell text.

Other text:

Sub GetParentShape()
    Dim oSh As Shape
    With ActiveWindow.Selection
        'Type might be None, Slides or one of the following:
        If .Type = ppSelectionShapes Then
            Set oSh = ActiveWindow.Selection.ShapeRange(1)
        End If
        If .Type = ppSelectionText Then
            Set oSh = ActiveWindow.Selection.TextRange.Parent.Parent
        End If

        Debug.Print oSh.Name

    End With
End Sub
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34