尝试运行以下宏时出现此错误。 该错误在第一次执行“ AddSlide”(!)时才会显示。宏继续(在消息框中单击“结束”后),并且运行良好,之后没有任何错误消息。
子PushChartsToPPT_1()
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptShp As PowerPoint.Shape
Dim EndTime As Single
Dim rng As Range
Dim cht As Chart
Dim ws As Worksheet
Dim i, j As Single
Dim MainWorkBook, tempWorkBook As Workbook
Dim tempSheet As Worksheet
Dim pptCL As CustomLayout
Dim myShape As Object
Dim DestinationPPT, str As String
Set MainWorkBook = ActiveWorkbook
'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
DestinationPPT = "Template.pptx"
Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only
'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL
ppt.Visible = msoTrue
For Each ws In MainWorkBook.Worksheets
For i = 1 To ws.ChartObjects.Count
'>>>>> error next line
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL) '>>>>>error on this line
pptSld.Select
Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
DoEvents
pptSld.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
Next i
Next ws
结束子
答案 0 :(得分:0)
上一个循环是否有可能结束,而不是在“标题和内容”幻灯片上执行“退出”?这意味着值pptCL是未定义的。但是,那将是错误的错误报告;使用pptCL undefined运行的那一行会导致“无效的过程调用或参数”。
但是奇怪的是,您的代码在打开powerpoint文件的那一行上对我来说就死了:
设置pptPres = ppt.Presentations.Open(DestinationPPT,True)'只读
...它死于该确切的“自动化错误”。我将代码与可以在其中打开PowerPoint文件的代码进行比较,并且该代码包含以下行:
ppt.Visible = msoTrue
...使Powerpoint应用程序可见,就在“打开”命令上方。当我将该行添加到您的代码中时,运行时该错误消失了。
这可能是完全的鲱鱼,但是由于它对我有用,并且由于您没有其他答案,请花点时间尝试一下。