形状名称PPT VBA

时间:2015-01-21 20:01:23

标签: vba powerpoint shapes

我试图在PPT VBA中通过索引引用形状。它正在工作,但它是一个命中和错过,因为我正在剪切并将它们粘贴到不同的幻灯片,因此索引不断变化。有没有其他方法来引用形状,所以我不必处理索引?以下是我的代码示例:

ActivePresentation.Slides(1).Shapes(3).Cut
ActivePresentation.Slides(2).Shapes.Paste
ActivePresentation.Slides(1).Shapes(4).Cut
ActivePresentation.Slides(2).Shapes.Paste

 With ActivePresentation.Slides(1).Shapes(3)
.Height = 325
.Width = 325
.Left = 190
.Top = 90

End With

With ActivePresentation.Slides(1).Shapes(4)
.Height = 600
.Width = 600
.Left = 65
.Top = 360

End With

1 个答案:

答案 0 :(得分:1)

在切割之前标记形状,然后调用函数以在粘贴后从目标幻灯片返回标记有该值的形状:

With ActivePresentation.Slides(1).Shapes(3)
  .Tags.Add "SomeName", "SomeValue" 
  ' whatever name and value you like
  .Cut
End With

' Then paste it onto another slide
' and to get a reference to it:

Dim oSh as shape
Set oSh = ShapeTaggedWith(oSl, "SomeName", "SomeValue")
If Not oSh is Nothing Then

End If
' etc

Function ShapeTaggedWith(oSl as Slide, sTagName as String sValue as String) as Shape
   Dim oSh as Shape
   For each oSh in oSl.Shapes
      If oSh.Tags(sTagName) = sValue Then
          Set ShapeTaggedWith = oSh
          Exit Function
      End If
   Next

End Function