benposaner
New Member
- Joined
- Jan 7, 2021
- Messages
- 9
- Office Version
- 365
- 2019
- Platform
- 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:
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?
This is the code I've been using.
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