Excel to copy and paste Ranges and Charts into Powerpoint

benposaner

New Member
Joined
Jan 7, 2021
Messages
9
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi

I've been trying to adapt the below code to also copy charts, as well as ranges.
It works fine for the range, but when I try to do a similar copy for a chart I'm getting a "Type Mismatch" error.

This is the line that errors:
VBA Code:
'Copy Chart from Excel
Set myChart = Worksheets("Sheet1").ChartObjects("Chart 2")

I've tried various Worksheet, Chart, ChartObjects combinations, but can't get it to work.
Sure it's a really simple answer, but any help would be great.

I'm also assuming the Chart Paste is not 100% correct, but I haven't obviously got that far to know :)
Does this look like it will paste the chart OK into PowerPoint?

VBA Code:
'Copy Excel Chart
myChart.CopyPicture

'Paste Chart to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set Chart position:
myShape.Left = 15
myShape.Top = 100




This is the code I've been using.

VBA Code:
Sub ExcelToPowerpoint()
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim DestinationPPT As String
Dim myShape As Object
Dim mySlide As Object
Dim myChart As Excel.Chart

'Copy Range from Excel
Set rng = Worksheets("Sheet1").Range("A1:D14")

'Copy Chart from Excel
Set myChart = Worksheets("Sheet1").ChartObjects("Chart 2")

'Create an Instance of PowerPoint
On Error Resume Next

'Set your destination path for the powerpoint presentation and open the file
Set PowerPointApp = CreateObject("Powerpoint.application")
DestinationPPT = ("C:\Monthly\Test01.pptx")
PowerPointApp.Presentations.Open (DestinationPPT)

'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
    MsgBox "Powerpoint could not be found.aborting."
    Exit Sub
End If
On Error GoTo 0

'Optimize Code
Application.ScreenUpdating = False

'Set my current Powerpoint window as activated
Set myPresentation = PowerPointApp.ActivePresentation

'Set which slide to paste into
Set mySlide = myPresentation.Slides(3)

'Copy Excel Range
rng.Copy

'Paste range to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set position:
myShape.Left = 15
myShape.Top = 15
myShape.Height = 250
myShape.Width = 750

'Copy Excel Chart
myChart.CopyPicture

'Paste Chart to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set Chart position:
myShape.Left = 15
myShape.Top = 100

'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate

'Clear The Clipboard
Application.CutCopyMode = False

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
2,769
Office Version
  1. 2013
Platform
  1. Windows
Declaring your chart as a shape and refering to it the same way should work:

VBA Code:
Dim myChart As Shape

Set myChart = Worksheets("Sheet1").Shapes("Chart 1")
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,661
Messages
5,838,648
Members
430,559
Latest member
jm5498

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
Top