4

I have this code to copy charts from an Excel 2010 worksheet into powerpoint. It loops through searches for all charts on the active worksheet then copy and pastes a link into powerpoint. There is also a small snippet of code that takes the chart title and puts it as a title into PowerPoint.

It works perfectly for me in most instances, however it is giving me a runtime error -2147467259 (80004005) Method 'PasteSpecial' of object 'Shapes' failed after 9 charts have been moved into powerpoint. What could be causing this failure in the middle of running perfectly?

Sub CreatePowerPoint()

 'Add a reference to the Microsoft PowerPoint Library by:

    Dim newPowerPoint As PowerPoint.Application
    Dim activeSlide As PowerPoint.Slide
    Dim cht As Excel.ChartObject

 'Look for existing instance
    On Error Resume Next
    Set newPowerPoint = GetObject(, "PowerPoint.Application")
    On Error GoTo 0

'Let's create a new PowerPoint
    If newPowerPoint Is Nothing Then
        Set newPowerPoint = New PowerPoint.Application
    End If
'Make a presentation in PowerPoint
    If newPowerPoint.Presentations.Count = 0 Then
        newPowerPoint.Presentations.Add
    End If

'Show the PowerPoint
    newPowerPoint.Visible = True

'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
    For Each cht In ActiveSheet.ChartObjects

    'Add a new slide where we will paste the chart
        newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
        newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
        Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

    'Copy the chart and paste it into the PowerPoint
        cht.Select
        ActiveChart.ChartArea.Copy
        activeSlide.Shapes.PasteSpecial(Link:=True).Select

    'Set the title of the slide the same as the title of the chart
        If ActiveChart.HasTitle = True Then
            activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text
        Else
            activeSlide.Shapes(1).TextFrame.TextRange.Text = "Add Title"
        End If
    'Adjust the positioning of the Chart on Powerpoint Slide
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 0.5 * 72
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 1.75 * 72
        newPowerPoint.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoFalse
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Height = 5.5 * 72
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 8.92 * 72

       Next

AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set newPowerPoint = Nothing

End Sub
1
  • 1
    After you paste, try Application.CutCopyMode = False to clear the clipboard maybe? Oct 4, 2013 at 16:36

3 Answers 3

4

The reason is very simple. You are not giving the Excel enough time to copy the chart to the clipboard.

Try this

    ActiveChart.ChartArea.Copy
    DoEvents
    activeSlide.Shapes.PasteSpecial(Link:=True).Select 
2
  • This solved it. Slows the process down significantly but is still far more efficient than me doing it by hand
    – mittence
    Oct 4, 2013 at 18:23
  • 1
    It will slow the process down as excel needs time to copy things to clipboard depending on the size of what you are copying :) Oct 4, 2013 at 18:24
1

You can try this as well, It worked for me, if not increase the seconds and see (not it is 1 sec, For me it worked for 2 secs.) Thanks, Syed.

ActiveChart.ChartArea.Copy
Application.Wait Now + TimeValue("00:00:01")
activeSlide.Shapes.PasteSpecial(Link:=True).Select 
0

Excellent! What would I do without Stackoverflow?

With Sheets("Step 2- GEs Eliminated") 'paste into the Step2 sheet .Cells(2, i * 4).Select Application.Wait Now + TimeValue("00:00:001") 'this line from Stackoverflow. ActiveSheet.Paste End With

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.