Export excel charts to Powerpoint

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
106
Office Version
  1. 2019
  2. 2016
Hi all

I'm trying to export all the charts in my workbook to PowerPoint for a report that I've set up. The problem is that unfortunately different people who use it have different versions of excel so I can't reference the Powerpoint object library.

The code I have though is causing PowerPoint to crash, when I paste I want to paste as a picture rather than embedding the chart I've tried various combinations of pastespecial but I can't seem to get it to work. Any help would be much appreciated,

Option Explicit


Dim pptApp As Object
Dim pptPres As Object
Dim pptSlide As Object
Dim pptSlideCount As Integer
Sub ChartsToPowerPoint()
Dim xSheet As Worksheet
Dim xChartsCount As Integer
Dim xChart As Object
Dim xActiveSlideNow As Integer
Dim MySlideArray As Variant
Dim MyRangeArray As Variant
Dim x As Long
Dim shp As Object
Dim sngDefaultSlideWidth As Long

Application.ScreenUpdating = False

On Error Resume Next
For Each xSheet In ActiveWorkbook.Worksheets
xChartsCount = xChartsCount + xSheet.ChartObjects.Count
Next xSheet
If xChartsCount = 0 Then
MsgBox "Sorry, there are no charts to export!"
Exit Sub
End If
Set pptApp = GetObject(, "PowerPoint.Application")
If pptApp Is Nothing Then
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPres = pptApp.Presentations.Add
Set pptSlide = pptPres.Slides.Add(1, 12)
Else
If pptApp.Presentations.Count > 0 Then
Set pptPres = pptApp.ActivePresentation
If pptPres.Slides.Count > 0 Then
xActiveSlideNow = pptApp.ActiveWindow.View.Slide.SlideIndex
Set pptSlide = pptPres.Slides(xActiveSlideNow)
Else
Set pptSlide = pptPres.Slides.Add(1, 12)
End If
Else
Set pptPres = pptApp.Presentations.Add
Set pptSlide = pptPres.Slides.Add(1, 12)
End If
End If
For Each xSheet In ActiveWorkbook.Worksheets
For Each xChart In xSheet.ChartObjects
Call pptFormat(xChart.Chart)
Next xChart
Next xSheet
For Each xChart In ActiveWorkbook.Charts
Call pptFormat(xChart)
Next xChart

sngDefaultSlideWidth = pptPres.PageSetup.SlideWidth
MySlideArray = Array(2, 3, 4, 5, 6, 7, 8, 9)
MyRangeArray = Array(Dashboard.Range("H9:M9"), Dashboard.Range("P9:T9"), _
Dashboard.Range("O20:T20"), Dashboard.Range("H20:M20"), Dashboard.Range("H32:M32"), Dashboard.Range("P32:T32"), Dashboard.Range("H47:M47"), Dashboard.Range("P47:T47"))

For x = LBound(MySlideArray) To UBound(MySlideArray)
MyRangeArray(x).Copy

On Error Resume Next
Set shp = pptPres.Slides(MySlideArray(x)).Shapes.PasteSpecial(DataType:=2)
On Error GoTo 0


With pptPres.PageSetup
shp.Left = (sngDefaultSlideWidth - shp.Width) / 4
shp.Top = (.SlideHeight / 8) - (shp.Height / 2)
shp.Height = 320
shp.Width = 580
End With

Next x


Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
MsgBox "The slide pack is now ready for editing!"
Application.ScreenUpdating = True


End Sub




Private Sub pptFormat(xChart As Chart)

Dim I As Integer
Dim sngDefaultSlideWidth As Long



On Error Resume Next
xChart.ChartArea.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
pptSlideCount = pptPres.Slides.Count

Set pptSlide = pptPres.Slides.Add(pptSlideCount + 1, 12)
pptSlide.Select
pptSlide.Shapes.Paste
sngDefaultSlideWidth = pptPres.PageSetup.SlideWidth

For I = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(I)
Select Case .Type
Case msoPicture:
.Top = 87.84976
.Left = (sngDefaultSlideWidth - .Width) / 4
.Height = 422.7964
.Width = 646.5262
End Select
End With
Next I


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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