簡體   English   中英

VBA PowerPoint 顯示和隱藏形狀

[英]VBA PowerPoint Display and Hide Shapes

我有幾個 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

但我只需要隱藏矩形和箭頭,僅此而已。

最好的問候彼得

以該循環為起點並在其中應用一些邏輯。 形狀有兩個可能有用的屬性, 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

這將隱藏活動演示文稿中所有幻燈片中的所有矩形類型和一部分箭頭類型:

' 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 屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM