Using VBA to paste chart into PowerPoint

merce33

Active Member
Joined
Oct 27, 2006
Messages
277
Can anyone tell me why the following code is generating this error:
Run time error ‘-2147188160 (80048420)’:

Slides (unknown member): invalid request. Clipboard is empty or contains data which may not be pasted here.

Code:
Sub testPPTcopyPaste()
    
    Dim wkbk As Workbook
    Dim sht As Worksheet
    Dim chrt As Chart
    Dim pptApp As PowerPoint.Application
    Dim pptStart As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    
    Set wkbk = ThisWorkbook
    Set sht = wkbk.Sheets("pivot table")
    
    
    
    Set pptStart = CreateObject("PowerPoint.Application")
    Set pptApp = GetObject(, "PowerPoint.Application")
    pptApp.Visible = True
    
    'create a new document
    Set pptPres = pptApp.Presentations.Add
    pptPres.Slides.Add(Index:=1, Layout:=ppLayoutTitle).Select
    
    pptPres.Slides(1).Shapes.Range(Array("Rectangle 2", "Rectangle 3")).Delete
    wkbk.Activate
    
    wkbk.Charts(1).ChartArea.Copy
    pptPres.Slides.Paste pptPres.Slides.Count
        
End Sub

The error comes from the very last line where I am trying to Paste the data. The XL workbook shows the graph as being in the "copied" state, and then if i go to PPT and press CTRL+V the graph pastes just fine.
Any suggestions?

Thanks.
Mike
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
This is redundant, you don't need two PowerPoint.Application object variables:

Code:
    Set pptStart = CreateObject("PowerPoint.Application") 
    Set pptApp = GetObject(, "PowerPoint.Application")

use this instead:

Code:
    On Error Resume Next
    ' Check whether PowerPoint is running
    Set PPApp = GetObject(, "PowerPoint.Application")
    If PPApp Is Nothing Then
        ' PowerPoint is not running, create new instance
        Set PPApp = CreateObject("PowerPoint.Application")
        PPApp.Visible = True
    End If
    On Error GoTo 0

your syntax is twisted:

Code:
    pptPres.Slides.Paste pptPres.Slides.Count

should be

Code:
    pptPres.Slides(pptPres.Slides.Count).Paste

or

Code:
    pptPres.Slides(pptPres.Slides.Count).Shapes.Paste
 
Upvote 0

Forum statistics

Threads
1,225,229
Messages
6,183,729
Members
453,185
Latest member
radiantclassy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top