我通过使用VBA在不同的幻灯片上粘贴图像来更新我的MS PowerPoint。
其余代码运行良好。我不能做的是删除所有幻灯片上的现有图像并粘贴新图像。目前,它将新图像粘贴到旧图像的顶部,但旧图像仍然存在。我正在使用下面的代码:
Dim pptApp As PowerPoint.Application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = msoTrue
xlApp.Worksheets(2).Range("M2:S12").Copy
Set shp1 = ActivePresentation.Slides(17).Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)
With shp1
.Left = 370
.Top = 100
.Height = 360
.Width = 340
End With
作为VBA的新手,我不知道如何在上面的代码中添加删除命令。任何帮助都将不胜感激。
发布于 2015-02-03 16:53:35
这个(谢谢,L42)将适用于幻灯片上的单个msoPicture形状,但是如果有多个形状,它可能会遗漏一些:
Dim s As Shape
For Each s In ActivePresentation.Slides(17).Shapes
If s.Type = 13 Then s.Delete '13 is msoPicture
Next
为什么?假设幻灯片上有三个形状。我们遍历“形状”集合,发现第一个形状是图片并删除它。现在,形状集合中有两个形状,但是VBA的计数器没有考虑集合计数中的更改。它查看集合中的第二个形状,但这是幻灯片上的第三个形状,因此代码将完全忽略#2形状。
使用这样的东西更可靠:
Dim x as Long
For x = ActivePresentation.Slides(17).Shapes.Count to 1 Step -1
If ActivePresentation.Slides(17).Shapes(x).Type = msoPicture Then
ActivePresentation.Slides(17).Shapes(x).Delete
End If
Next
发布于 2015-02-02 09:50:12
Edit1:,正如史蒂夫所指出的,第一个发布的解决方案是不可靠的;正如道格在POST中所确认的那样。
要删除所有的图片使用循环,采取史蒂夫的方法,在他的文章解释。
现在,如果您只想删除所有图片,可以尝试如下:
ActivePresentation.Slides(17).Shapes.Range.Delete
但是这会删除所有的形状,不仅是图片,还有文本框、线条、形状等等。
要只删除图片,下面是使用循环的另一种方法。
Dim s As Shape, pictodel As Variant
For Each s In ActivePresentation.Slides(17).Shapes
If s.Type = 13 Then
If IsArray(pictodel) Then
ReDim Preserve pictodel(UBound(pictodel) + 1)
pictodel(UBound(pictodel)) = s.Name
Else
pictodel = Array(s.Name)
End If
End If
Next
ActivePresentation.Slides(17).Shapes.Range(pictodel).Delete
希望这会有所帮助,但更简单的解决方案将是Steve的。)
https://stackoverflow.com/questions/28274753
复制相似问题