从 excel 的 powerpoint 中删除幻灯片中的图片

delete pictures from slides in powerpoint from excel

我试图找出编码不起作用的原因,但以下代码应该从 excel 幻灯片打开并清除现有幻灯片以替换为新图片 - 但是我得到以下内容:

error 91: Object variable or with block variable not set.

我尝试了 Stack 中的其他几个代码,但无法使其正常工作.. 有什么帮助吗?甲板包含幻灯片 2 到幻灯片 9 以清除。

Sub ppt_export()
Dim DestinationPPT As String
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim objApp As Object, objSlide As Object, ObjShp As Object, objTable As` Object

DestinationPPT = "C:\Users\Desktop\Summary.pptx"
Set ppApp = CreateObject("PowerPoint.Application")
Set ppPres = ppApp.Presentations.Open(DestinationPPT)
'delete the shapes from the renew the template

 For i = ppSlide.Shapes.Count To 1 Step -1
Set ppShape = ppSlide.Shapes(p)
If ppShape.Type = msoPicture Then ppShape.Delete
Next
End Sub

我想知道如何更正代码以便继续编码,将 excel 工作表作为图片复制到相应的幻灯片中。

首先也是最重要的,将Option Explicit添加到代码模块的顶部,它会标记您拥有的各种未声明的变量:p , i, ppSlide, 和 ppShape.

那么代码可能看起来像这样:

Option Explicit

Sub ExportToPPT()
    Dim ppApp As PowerPoint.Application
    Set ppApp = New PowerPoint.Application

    Dim ppFileName As String
    ppFileName = "C:\Users\Desktop\Summary.pptx"

    Dim ppPres As PowerPoint.Presentation
    Set ppPres = ppApp.Presentations.Open(Filename:=ppFileName)

    Dim ppSlide As PowerPoint.Slide

    Dim i As Integer
    For i = 2 To 9
        Set ppSlide = ppPres.Slides(i)

        Dim j As Integer
        For j = ppSlide.Shapes.Count To 1 Step -1
            If ppSlide.Shapes(j).Type = msoPicture Then
                ppSlide.Shapes(j).Delete
            End If
        Next j
    Next i
End Sub