Powerpoint中形状的存在 [英] Existence of shapes in Powerpoint
问题描述
我想在启用宏的 powerpoint 演示文稿上的命令按钮上建立条件.如果该形状存在,那么我希望将其删除,否则该按钮应生成关于不存在此类形状的声明.目前我的存在有问题......!如何让 Power point 识别形状为空?这是我的代码:
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 produce a statement about the fact that there is no such shape. Currently I am having trouble with existence...! How do I get Power point to recognise the shape is null? Here is my code:
If ActivePresentation.Slides(3).Shapes("Picture") Is Nothing Then
MsgBox "no Picture"
Else
ActivePresentation.Slides(3).Shapes("Picture").Delete
MsgBox "Picture Cleared"
End If
这段代码只会产生一个错误,因为形状不存在所以第一个 if 语句失败.也许我们需要检查它是否在选择窗格中?
This code only produces an error because the shape doesn't exist so the first if statement fails. Perhaps we need to check whether its in the selection pane?
推荐答案
正如@davidmneedham 在评论中的链接中给出的那样 (@TimWilliams 回答),您可以使用类似如下的结构:
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
这篇关于Powerpoint中形状的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!