PDA

View Full Version : Copying chart from Excel to PowerPoint



almumoha
12-19-2023, 05:27 AM
Hello everyone,

Question ; what's the VBA code to copy a chart or more than a chart from Excel to PowerPoint?

Thanks

Aussiebear
12-19-2023, 11:22 AM
Welcome to VBAX almumoha. Try this


Const ppLayoutBlank = 2
Const ppViewSlide = 1

Sub ExportChartstoPowerPoint()
Dim PPApp As Object
Dim chr
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Presentations.Add
PPApp.ActiveWindow.ViewType = ppViewSlide
For Each chr In Sheets("Chart1").ChartObjects
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
chr.Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPApp.ActiveWindow.View.Paste
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Next chr
PPApp.Visible = True
EndSub

LucyHills
12-22-2023, 08:32 AM
You can use VBA (Visual Basic for Applications) to copy a chart or multiple charts from Excel to PowerPoint. Below is a simple example of VBA code that you can use as a starting point:

Sub CopyChartsToPowerPoint()
Dim PowerPointApp As Object
Dim PowerPointSlide As Object
Dim ExcelChart As ChartObject
Dim slideIndex As Integer
' Create a new instance of PowerPoint
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True ' Show PowerPoint application
' Add a new presentation
Set PowerPointPresentation = PowerPointApp.Presentations.Add
' Loop through all chart objects in the active sheet
For Each ExcelChart In ActiveSheet.ChartObjects
' Add a new slide to the PowerPoint presentation
Set PowerPointSlide = PowerPointPresentation.Slides.Add(slideIndex + 1, ppLayoutText)
slideIndex = slideIndex + 1
' Copy the chart to the clipboard
ExcelChart.CopyPicture
' Paste the chart onto the PowerPoint slide
PowerPointSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Selec t
' Adjust the position and size of the pasted chart as needed
' For example, you can use the following lines to move the chart to a specific position:
' PowerPointApp.ActiveWindow.Selection.ShapeRange.Left = 100
' PowerPointApp.ActiveWindow.Selection.ShapeRange.Top = 100
Next ExcelChart
' Clean up PowerPoint objects
Set PowerPointApp = Nothing
Set PowerPointPresentation = Nothing
Set PowerPointSlide = Nothing
End Sub