我试图弄清楚为什么编码不起作用,但下面的代码应该从excel打开一个powerpoint并清除现有的幻灯片以替换新的图片 - 但是我得到以下内容:
错误91:对象变量或未设置块变量。
我从堆栈中尝试了其他几个代码,但无法使其工作..有什么帮助吗?平台包含滑动件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