Powerpoint 中形状的存在
Existence of shapes in Powerpoint
我想在启用宏的 powerpoint 演示文稿的命令按钮上构建条件。如果该形状存在,那么我希望将其删除,否则该按钮应生成一个声明,说明不存在这种形状。目前,我在生存方面遇到了麻烦......!如何让 Power Point 识别形状为空?这是我的代码:
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 语句失败。也许我们需要检查它是否在选择窗格中?
正如@davidmneedham 在评论 (@TimWilliams answer) 中的 link 中给出的那样,您可以使用类似于如下的结构:
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
使用与该答案相同的格式:
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
其他一些建议会奏效,但总的来说,除非绝对必要,否则依赖选择是一种不好的做法。相反,您可以调用一个稍微不同的函数:
Function ShapeExists(ByVal oSl as Slide, ByVal ShapeName as String) as Boolean
Dim oSh as Shape
For Each oSh in oSl.Shapes
If oSh.Name = ShapeName Then
ShapeExists = True
Exit Function
End If
Next ' Shape
' No shape here, so though it's not strictly necessary:
ShapeExists = False
End Function
您也可以将其修改为 return 找到形状的引用,如果找不到则不引用。
如果您不想使用提前退出函数,有一些简单的方法可以解决这个问题。
我想在启用宏的 powerpoint 演示文稿的命令按钮上构建条件。如果该形状存在,那么我希望将其删除,否则该按钮应生成一个声明,说明不存在这种形状。目前,我在生存方面遇到了麻烦......!如何让 Power Point 识别形状为空?这是我的代码:
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 语句失败。也许我们需要检查它是否在选择窗格中?
正如@davidmneedham 在评论 (@TimWilliams answer) 中的 link 中给出的那样,您可以使用类似于如下的结构:
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
使用与该答案相同的格式:
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
其他一些建议会奏效,但总的来说,除非绝对必要,否则依赖选择是一种不好的做法。相反,您可以调用一个稍微不同的函数:
Function ShapeExists(ByVal oSl as Slide, ByVal ShapeName as String) as Boolean
Dim oSh as Shape
For Each oSh in oSl.Shapes
If oSh.Name = ShapeName Then
ShapeExists = True
Exit Function
End If
Next ' Shape
' No shape here, so though it's not strictly necessary:
ShapeExists = False
End Function
您也可以将其修改为 return 找到形状的引用,如果找不到则不引用。
如果您不想使用提前退出函数,有一些简单的方法可以解决这个问题。