使用VBA将新的图像替换为MS PowerPoint中的现有图像 [英] Replace existing image in MS PowerPoint with a new image using VBA
问题描述
我正在使用VBA在不同的幻灯片上粘贴图像来更新我的MS PowerPoint。
I'm updating my MS PowerPoint by pasting images on different slides using VBA.
其余的代码正常工作。我无法做的是删除所有幻灯片上的现有图像并粘贴新图像。目前,它将新图像粘贴在旧图像之上,但仍旧存在旧图像。我使用以下代码:
Rest of the code is working fine. What I'm unable to do is delete the existing image on all the slides and paste the new image. Currently it paste the new image on top of old image, but old image remains. I'm using below code:
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的新手,我不知道哪里以及如何在上面的代码中添加delete命令。任何一种帮助将不胜感激。
Being a newbie to VBA, I dont know where and how to add delete command in above code. Any kind of help would be appreciated.
推荐答案
这(谢谢L42)将适用于幻灯片上的单个msoPicture形状,但如果有多个形状,可能会错过一些:
This (thanks, L42) will work for single msoPicture shapes on a slide, but if there's more than one shape, it may miss some:
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。
Why? Suppose you have three shapes on the slide. We iterate through the shapes collection, find that the first shape is a picture and delete it. Now there are two shapes in the shapes collection, but VBA's counter doesn't take account of changes in the collection count. It looks at the second shape in the collection, but that's now what WAS the third shape on the slide, so the code will miss shape #2 altogether.
它更可靠使用这样的东西:
It's more reliable to use something like this:
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
这篇关于使用VBA将新的图像替换为MS PowerPoint中的现有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!