vbavstopowerpointpowerpoint-2010

Starting with a TextRange, how do I find the enclosing Shape?


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.


Solution

  • 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