按名称查找一些形状,按特定颜色查找其他形状并删除它们
Find some shapes by name, other shapes by specific color and delete them
我有一些需要删除形状的演示文稿,
- 特定的.Name
- 具体颜色
那些具有特定 .Name 的形状可以分组(不在我的代码中)。我在Whosebug中找到代码并尝试修改它。
- 按名称查找形状并将其删除:具体名称可以是“XXName1”和“Name1”。
如果没有带有 .Name = "Name1"
的形状,我会得到一个错误
"Object does not exist"
上线If .Name = "Name1" Or .Name = "Name2" Then
有时代码可以工作,然后,如果演示文稿中有很多幻灯片,我就会出错。
当我用 1 张幻灯片演示进行测试时 - 没有错误。
- 按颜色查找形状并删除:
我有一个错误
"Object variable or With block variable not set"
我不明白如何声明变量
Sub DeleteShapes()
Dim oSld As Slide
Dim oShp As Shape
Dim oshpGroup As Shape
Dim Y As Long
Dim L As Long
Dim str As String
For Each oSld In ActivePresentation.Slides
For L = oSld.Shapes.Count To 1 Step -1
With oSld.Shapes(L)
' Find shape by name and delete it
If .Name = "XXName1" Or .Name = "XXName2" Then
.Delete
End If
If .Name = "Name1" Or .Name = "Name2" Then
.Delete
End If
' Find shape by color and delete it
If oShp.Fill.ForeColor.RGB = RGB(0, 0, 0) Or _
oShp.Fill.ForeColor.RGB = RGB(1, 1, 1) Or _
oShp.Fill.ForeColor.RGB = RGB(2, 2, 2) Or _
oShp.Fill.ForeColor.RGB = RGB(3, 3, 3) Then
oShp.Delete
End If
End With
Next L
Next oSld
End Sub
You can't refer to a shape after you've deleted it (which you've done
previously). Change your sequential If...End If, If...End If to
If...ElseIf....ElseIf...End If. – @BigBen
我修改后的代码:
Sub DeleteShapes()
Dim oSld As Slide
Dim oShp As Shape
Dim L As Long
For Each oSld In ActivePresentation.Slides
For L = oSld.Shapes.Count To 1 Step -1
With oSld.Shapes(L)
If .Name = "XXName1" Or .Name = "XXName2" Then
.Delete
ElseIf .Name = "Name1" Or .Name = "Name2" Then
.Delete
ElseIf .Fill.ForeColor.RGB = RGB(0, 0, 0) Then
.Delete
ElseIf .Fill.ForeColor.RGB = RGB(1, 1, 1) Then
.Delete
ElseIf .Fill.ForeColor.RGB = RGB(2, 2, 2) Then
.Delete
ElseIf .Fill.ForeColor.RGB = RGB(3, 3, 3) Then
.Delete
End If
End With
Next L
Next oSld
End Sub
我有一些需要删除形状的演示文稿,
- 特定的.Name
- 具体颜色
那些具有特定 .Name 的形状可以分组(不在我的代码中)。我在Whosebug中找到代码并尝试修改它。
- 按名称查找形状并将其删除:具体名称可以是“XXName1”和“Name1”。
如果没有带有 .Name = "Name1"
的形状,我会得到一个错误
"Object does not exist"
上线If .Name = "Name1" Or .Name = "Name2" Then
有时代码可以工作,然后,如果演示文稿中有很多幻灯片,我就会出错。 当我用 1 张幻灯片演示进行测试时 - 没有错误。
- 按颜色查找形状并删除:
我有一个错误
"Object variable or With block variable not set"
我不明白如何声明变量
Sub DeleteShapes()
Dim oSld As Slide
Dim oShp As Shape
Dim oshpGroup As Shape
Dim Y As Long
Dim L As Long
Dim str As String
For Each oSld In ActivePresentation.Slides
For L = oSld.Shapes.Count To 1 Step -1
With oSld.Shapes(L)
' Find shape by name and delete it
If .Name = "XXName1" Or .Name = "XXName2" Then
.Delete
End If
If .Name = "Name1" Or .Name = "Name2" Then
.Delete
End If
' Find shape by color and delete it
If oShp.Fill.ForeColor.RGB = RGB(0, 0, 0) Or _
oShp.Fill.ForeColor.RGB = RGB(1, 1, 1) Or _
oShp.Fill.ForeColor.RGB = RGB(2, 2, 2) Or _
oShp.Fill.ForeColor.RGB = RGB(3, 3, 3) Then
oShp.Delete
End If
End With
Next L
Next oSld
End Sub
You can't refer to a shape after you've deleted it (which you've done previously). Change your sequential If...End If, If...End If to If...ElseIf....ElseIf...End If. – @BigBen
我修改后的代码:
Sub DeleteShapes() Dim oSld As Slide Dim oShp As Shape Dim L As Long For Each oSld In ActivePresentation.Slides For L = oSld.Shapes.Count To 1 Step -1 With oSld.Shapes(L) If .Name = "XXName1" Or .Name = "XXName2" Then .Delete ElseIf .Name = "Name1" Or .Name = "Name2" Then .Delete ElseIf .Fill.ForeColor.RGB = RGB(0, 0, 0) Then .Delete ElseIf .Fill.ForeColor.RGB = RGB(1, 1, 1) Then .Delete ElseIf .Fill.ForeColor.RGB = RGB(2, 2, 2) Then .Delete ElseIf .Fill.ForeColor.RGB = RGB(3, 3, 3) Then .Delete End If End With Next L Next oSld End Sub