Run time error '-2147188160 (80048240)': A

2019-08-20 09:40发布

I am getting this error while trying to run the following macro. The error shows up on the first(!) time the "AddSlide" is executed and only then(!). The macro continues (after I click on "End" in the message box) and works well without any error message after that.

Sub 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

End Sub

1条回答
唯我独甜
2楼-- · 2019-08-20 10:26

Is it possible that the previous loop is finishing, instead of doing an Exit For at a 'title and content' slide? That would mean that the value pptCL is undefined. However, that would be the wrong error report; that line run with pptCL undefined causes an "invalid procedure call or argument".

But the odd thing is that your code died for me right on the line where the powerpoint file is opened, the line:

Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only

...and it died with that exact "automation error". I compared to code I have where a powerpoint file opened fine, and that code had the line:

ppt.Visible = msoTrue

...making the powerpoint application visible, just above the "open" command. When I added that line to your code, the error went away, in my run.

This may be a complete red herring, but since it worked for me, and since you aren't getting any other answers, take a moment and try it.

查看更多
登录 后发表回答