The below Sub is supposed to paste an Excel chart 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
I get a Run-time error:
Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here.
At the line:
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
Error http://im64.gulfup.com/pZNwxJ.png
I tried pptSlide.Shapes.Paste
but it gives the same error.
When I amend pptApp.Presentations.Add(msoFalse)
to pptApp.Presentations.Add
only it works but the PowerPoint App is displayed.
When I change 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.
.Chart.Export FileName:="C:\Users\xyz\Desktop\1.png, FilterName:="PNG"
will work just fine. However in Excel 2007 SP3 the images of the charts produced using this method are of a really bad quality. For some reason when pasting it and saving from PowerPoint it's much better and hence the reason for my above approach. – CaptainABCpptApp.CommandBars.ExecuteMso "PastePng"
method instead ofPasteSpecial
. – David ZemenspptApp.CommandBars.ExecuteMso "PastePng"
gives Method 'ExecuteMso' of object '_CommandBars' failed. Also it there a working way to paste the chart itself instead of pasting it as PNG? – CaptainABCpptApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"
fails giving Method 'ExecuteMso' of object '_CommandBars' failed. Here is a link to a my sample sheet: db.tt/nGrgF5bA – CaptainABC