我将图像从文件夹导入到 PowerPoint 幻灯片上。我发现如果我重新运行宏,它每次都会添加相同的图片(即,如果我运行宏四次,就会有四张相同的图片)。我需要运行这个宏几次。
我想添加一行,如果图片已经存在,则在再次添加之前将其删除,但是
.Delete
仅在您知道特定图像名称(即“图片 2”)时才有效。每次通过宏导入图像时,它们都会被赋予一些随机数。
有什么方法可以在导入时控制图片的数量(即指定“图片1”)或避免重复添加相同的图片?
Set plot1 = .Shapes.AddPicture(imagePath & "image.png", LinkToFile = False, SaveWithDocument = True, Left:=100, Top:=100, Width:=180, Height:=160)
Sub Demo()
Dim plot1 As Shape, imagePath As String, bFound As Boolean
Const IMG_NAME = "MyPic"
With ActiveWindow.Presentation.Slides(1)
For Each plot1 In .Shapes
If plot1.Name = IMG_NAME Then
bFound = True
Exit For
End If
Next
If Not bFound Then
imagePath = "d:\temp\"
Set plot1 = .Shapes.AddPicture(imagePath & "image.png", _
LinkToFile:=False, SaveWithDocument:=True, _
Left:=100, Top:=100, Width:=180, Height:=160)
plot1.Name = "MyPic"
End If
End With
End Sub
Sub Demo()
Dim plot1 As Shape, imagePath As String
Const IMG_NAME = "MyPic"
With ActiveWindow.Presentation.Slides(1)
For Each plot1 In .Shapes
If plot1.Name = IMG_NAME Then
plot1.Delete
Exit For
End If
Next
imagePath = "d:\temp\"
Set plot1 = .Shapes.AddPicture(imagePath & "image.png", _
LinkToFile:=False, SaveWithDocument:=True, _
Left:=100, Top:=100, Width:=180, Height:=160)
plot1.Name = "MyPic"
End With
End Sub