从TextRange开始,如何找到封闭的Shape?

时间:2013-04-30 15:58:23

标签: vsto powerpoint powerpoint-vba powerpoint-2010

假设我有TextRange个对象,我需要找到包含Shape的{​​{1}}。

通常,我可以使用TextRange的{​​{1}}属性来获取包含它的Parent,然后再次使用TextRange属性来获取TextFrame 1}}。

,如果文字位于表格单元格内,则Parent的{​​{1}}属性为Shape。 (我认为这是PowerPoint 2010的“功能”)。 编辑:除非通过Selection.TextRange访问TextRange,否则不是这样。

还有其他方法可以识别形状(在本例中是表格单元格)吗?

更新:感谢KazJaw,我再次看了一遍,结果发现我可以导航Parent链,除非{{1我是从TextRange获得的。就我的目的而言,这不是一个问题。

2 个答案:

答案 0 :(得分:1)

基于下面评论中的进一步讨论问题,似乎真正的问题是指选择对象。如果选择表格中的任何文本,则立即进行的某些测试会产生以下结果:

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

其他信息也适用于其他程序员。以下我发现有点混乱做一些测试来回答这个问题。 (对于简单的演示文稿,一张幻灯片,其中一个表格和一些填充文本的单元格)

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

答案 1 :(得分:0)

并不是因为你无法通过选择到达那里,有时候你选择了什么。对象模型已破坏表格单元格中的文本。如上所述,ActiveWindow.Selection.TextRange.Parent不为选定的表格单元格文本返回任何内容。

其他文字:

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
相关问题