Existence of shapes in Powerpoint

前端 未结 2 1304
醉话见心
醉话见心 2021-01-24 11:04

I would like to build a condition on a command button on a Macro enabled powerpoint presentation. If the shape exists then I would like it deleted, otherwise the button should p

2条回答
  •  情书的邮戳
    2021-01-24 11:48

    As @davidmneedham gives in the link in the comments (@TimWilliams answer), you can use a construct similar to as follows:

    Option Explicit
    
    Sub test()
    
    Dim shp As Shape
    Dim myshapeName As String
    myshapeName = "Picture"
    Dim sl As Slide
    
    Set sl = ActivePresentation.Slides(1)
    
     If shapePresent(sl, myshapeName) Then
    
         sl.Shapes(myshapeName).Delete
    
     Else
    
        MsgBox myshapeName & " not present"
    
     End If
    
    End Sub
    
    
    Private Function shapePresent(ByVal sl As Slide, ByVal myshapeName As String) As Boolean
    
       On Error GoTo errhand
    
       sl.Shapes(myshapeName).Select
    
       shapePresent = True
       Exit Function
    
    errhand:
    
    shapePresent = False
    Err.Clear
    
    End Function
    

    Using the same format as that answer:

    Private Function shapePresent(ByVal sl As Slide, ByVal myshapeName As String) As Boolean
    
        Dim myShape As Shape
    
        On Error Resume Next
    
        Set myShape = sl.Shapes(myshapeName)
    
        On Error GoTo 0
    
        shapePresent = Not myShape Is Nothing
    
    End Function
    

提交回复
热议问题