Error Pasting Excel Chart into Powerpoint VBA

浪子不回头ぞ 提交于 2019-12-17 20:27:19

问题


I have the below Sub which is suppose to take a chart from excel and paste it into a newly created PowerPoint slide. It then exports the chart as a PNG:

Sub ChartsToPowerPoint()

    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide

    'Open PowerPoint and create an invisible new presentation.
    Set pptApp = New PowerPoint.Application
    Set pptPres = pptApp.Presentations.Add(msoFalse)

    'Set the charts and copy them to a new ppt slide

    Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    objChart.ChartArea.Copy
    Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse

    'Save Images as png
    path = "C:\Users\xyz\Desktop\"

    For j = 1 To pptSlide.Shapes.Count
        With pptSlide.Shapes(j)
        .Export path & j & ".png", ppShapeFormatPNG
        End With
    Next j

    pptApp.Quit

    Set pptSlide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing

End Sub

When I run the above code I get a Run-time error: Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here. This is at the line:

pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse

Error http://im64.gulfup.com/pZNwxJ.png

I have also tried pptSlide.Shapes.Paste but it gives the same error.

However when I amend pptApp.Presentations.Add(msoFalse) to pptApp.Presentations.Add only it works OK but the PowerPoint App is displayed.

Interestingly, when I change the to .PasteSpecial DataType:=ppPasteEnhancedMetafile or .PasteSpecial DataType:=ppPastePNG everything runs smoothly even with .Add(msoFalse).

I am thinking it might be something to do with setting the focus or so. Any clues?


回答1:


PasteSpecial and CommandBars.ExecuteMso should both work (tested your code in Excel/PowerPoint 2010 with the following caveat:

When you add presentation, you have to open it WithWindow:=True

Set pptPres = pptApp.Presentations.Add(msoCTrue)

I did some more digging, you need to use the CopyPicture method and then I think you can open withwindow=False. Try:

Sub ChartsToPowerPoint()

    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide
    Dim objChart As Chart

    'Open PowerPoint and create an invisible new presentation.
    Set pptApp = New PowerPoint.Application
    Set pptPres = pptApp.Presentations.Add(msoFalse)

    Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    objChart.CopyPicture

    Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse

    'Save Images as png
    Path = CreateObject("Wscript.Shell").SpecialFolders("Desktop") & "\"

    For j = 1 To pptSlide.Shapes.Count
        With pptSlide.Shapes(j)
        .Export Path & j & ".png", ppShapeFormatPNG
        End With
    Next j

    pptApp.Quit

    Set pptSlide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing

End Sub



回答2:


This is a common error we can experience when we copy information from one Office application to another. The best way I can describe it is that the program runs too fast and the information we copy never actually makes it into our clipboard.

This means that when we go and try to paste it, we get an error because there is nothing in our clipboard to paste.

Now lucky for us there is a way to fix this error but it requires us to add an extra line of code.

'Copy the chart
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    objChart.CopyPicture

'Pause the application for ONE SECOND
Application.Wait Now + #12:00:01 AM#

'Paste your content into a slide as the "Default Data Type"
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse

Now all I did was add an extra line of code that pauses your Excel application for one second. This will give it enough time to make sure that the information is stored in your clipboard.

You might be asking the question why does this happen sometimes but then not other times. Well, it just boils down to this the clipboard can act unpredictably and clear out information inside of it.

That's why if we can avoid storing the information in the clipboard we try to. However, in this case, we can't avoid it so we just have to live with the unpredictability.




回答3:


@areed1192's answer might work if PowerPoint had an Application.Wait message, but that's an Excel thing.

I was able to do something similar by using a technique found here:

Which is to say, put a the top of the module:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

And then call it like this:

Sleep 1000
DoEvents

(I'm not positive the DoEvents helped, but it seemed like it might be a good idea for resolving a race condition in VBA if that's what's going on.)




回答4:


sld.Shapes.PasteSpecial DataType:=0

or

sld.Shapes.PasteSpecial DataType:=ppPasteShape



来源:https://stackoverflow.com/questions/25433826/error-pasting-excel-chart-into-powerpoint-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!