通过其ID访问Shape对象

时间:2015-07-25 05:39:48

标签: vba shape powerpoint-vba

我有一张包含5个形状的PowerPoint幻灯片。我想在宏中使用这些形状做不同的事情。如何使用形状ID更改其中一个形状?例如,我有两个名称为"Title 1"的形状,但我想使用ID为15的形状。

这是我的代码:

Sub size_n_spread_v()

    Dim j As Integer
    Dim sld As Slide
    Dim SldId As Long
    gap = std_gap

    SldId = ActiveWindow.View.Slide.SlideIndex
    Set sld = ActivePresentation.Slides(SldId)

    Call SortMultArray

    new_dim = (total_dim - gap * (lngRow - 1)) / lngRow

    'This works but is not specific:
    'sld.Shapes.("Title 1").Height = new_dim 

    'This would hopefully be specific but the syntax does not work Please HELP!
    'sld.Shapes.("Title 1").Id(15).Height = new_dim

End Sub

有人知道通过ID更改形状的正确语法吗?

2 个答案:

答案 0 :(得分:1)

我不知道某种方式,但您可以编写一个简单的帮助函数,然后您可以在整个项目中使用它来使自己更容易。像这样的东西会起作用:

Public Function GetShapeById(s As Slide, n As String, id As Long) As Shape

    Dim objShape As Shape
    For Each objShape In s.Shapes
        If StrComp(objShape.Name, n, vbTextCompare) = 0 And objShape.Id = id Then
            Set GetShapeById = objShape
            Exit Function
        End If
    Next

End Function

然后你可以像这样使用它:

Sub size_n_spread_v()

    ....

    ' Instead of:
    sld.Shapes.("Title 1").Id(15).Height = new_dim

    ' Use:
    GetShapeById(sld, "Title 1", 15).Height = new_dim

End Sub

答案 1 :(得分:0)

上面提到的功能是通过Id获取形状的唯一方法。您必须搜索Shapes集合,因为没有与SlideIndex相同的ShapeIndex。找到特定形状的另一个解决方案是通过添加自己的标签来唯一地识别形状,但这是一个更复杂的解决方案。