VBA PowerPoint显示和隐藏形状

时间:2017-01-27 09:41:54

标签: vba powerpoint powerpoint-vba

我有几个带有对象(箭头和矩形)的PowerPoint幻灯片我喜欢显示和隐藏。目前我只是使用

ActivePresentation.Slides("Slide100").Shapes("Rectangle 99").Visible = False or True

ActivePresentation.Slides("Slide100").Shapes("Straight Arrow Connector 118").Visible = False or True

现在可以在该模板中删除一个矩形或箭头。当您运行宏时,这会导致VBA错误,因为无法找到矩形或箭头。有没有办法编写一个宏来检查所有使用过的矩形和箭头然后隐藏或显示它们而不是使用单个变量?

我发现了这样的事情:

For Each sObject In ActivePresentation.Slides(2).Shapes
sObject.Visible = False
Next

但我只需隐藏矩形和箭头,仅此而已。

祝你好运 彼得

2 个答案:

答案 0 :(得分:2)

将该循环作为起点并在其中应用一些逻辑。形状有两个可能有用的属性,autoshapetypename

以下两个例子:

For Each shp In ActivePresentation.Slides(x).Shapes
    If InStr(1, shp.Name, "Rectangle") > 0 Then
        shp.Visible = False
    End If
Next shp

For Each shp In ActivePresentation.Slides(x).Shapes
    If shp.AutoShapeType = msoShapeRectangle Then
        shp.Visible = False
    End If
Next shp

答案 1 :(得分:1)

这将隐藏活动演示文稿中所有幻灯片的所有矩形类型和箭头类型的子集:

' PowerPoint VBA Macro
' Purpose : hide rectangles and shapes across slides
' Written by : Jamie Garroch of YOUpresent Ltd. http://youpresent.co.uk.
Sub HideRectanglesAndArrows()
  Dim oSld As Slide
  Dim oShp As Shape
  For Each oSld In ActivePresentation.Slides
    For Each oShp In oSld.Shapes
      If oShp.Type = msoAutoShape Then
        Select Case oShp.AutoShapeType
          ' Basic Arrows (4)
          Case msoShapeUpArrow, msoShapeDownArrow, msoShapeLeftArrow, msoShapeRightArrow
            oShp.Visible = msoFalse
          ' Double Arrows (2)
          Case msoShapeUpDownArrow, msoShapeLeftRightArrow
            oShp.Visible = msoFalse
          ' Add other arrow types as required
          '
          ' Basic Rectangles (1)
          Case msoShapeRectangle
            oShp.Visible = msoFalse
          ' Rounded Rectangles (4)
          Case msoShapeRound1Rectangle, msoShapeRound2DiagRectangle, msoShapeRound2SameRectangle, msoShapeRoundedRectangle
            oShp.Visible = msoFalse
          ' Snipped Rectangles (4)
          Case msoShapeSnip1Rectangle, msoShapeSnip2DiagRectangle, msoShapeSnip2SameRectangle, msoShapeSnipRoundRectangle
            oShp.Visible = msoFalse
        End Select
      End If
    Next
  Next
End Sub

然后,您可以使用.Name属性或位置属性(.Left,.Top)或大小属性(.Width,.Height)添加逻辑以删除特定形状。如果你想要更精细(用户可以更改形状的名称),那么你可以添加标签到形状,以用户无法更改的方式识别它们,然后编写一个过程来检查逻辑中的.Tags属性。 / p>