Aussiebear
05-02-2024, 03:09 AM
The following code sends a chart to a new instance of Powerpoint. How can it be modified to send to an existing presentation?
Sub ExportChartsToPowerPoint()
Dim pptApp As Object
Dim pptPres As Object
Dim chartObj As ChartObject
Dim slideIndex As Integer
' Create a new instance of PowerPoint application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True ' Make PowerPoint visible
' Create a new presentation
Set pptPres = pptApp.Presentations.Add
slideIndex = 1 ' Initialize slide index
' Loop through each chart object in the worksheet
For Each chartObj In Sheets("Slide 11").ChartObjects
With chartObj.Chart
' Copy the chart
.ChartArea.Copy
' Paste the chart into PowerPoint
pptPres.Slides.Add slideIndex, 1 ' Add a new slide
pptPres.Slides(slideIndex).Shapes.PasteSpecial
' Adjust the position and size of the pasted chart
With pptPres.Slides(slideIndex).Shapes(1)
.Left = 100 ' Adjust the left position as needed
.Top = 100 ' Adjust the top position as needed
.Width = 400 ' Adjust the width as needed
.Height = 300 ' Adjust the height as needed
End With
slideIndex = slideIndex + 1 ' Increment slide index
End With
Next chartObj
' Clean up
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
Actually, on second thoughts, how could it be modified to send a selected chart to a selected powerpoint presentation?
Sub ExportChartsToPowerPoint()
Dim pptApp As Object
Dim pptPres As Object
Dim chartObj As ChartObject
Dim slideIndex As Integer
' Create a new instance of PowerPoint application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True ' Make PowerPoint visible
' Create a new presentation
Set pptPres = pptApp.Presentations.Add
slideIndex = 1 ' Initialize slide index
' Loop through each chart object in the worksheet
For Each chartObj In Sheets("Slide 11").ChartObjects
With chartObj.Chart
' Copy the chart
.ChartArea.Copy
' Paste the chart into PowerPoint
pptPres.Slides.Add slideIndex, 1 ' Add a new slide
pptPres.Slides(slideIndex).Shapes.PasteSpecial
' Adjust the position and size of the pasted chart
With pptPres.Slides(slideIndex).Shapes(1)
.Left = 100 ' Adjust the left position as needed
.Top = 100 ' Adjust the top position as needed
.Width = 400 ' Adjust the width as needed
.Height = 300 ' Adjust the height as needed
End With
slideIndex = slideIndex + 1 ' Increment slide index
End With
Next chartObj
' Clean up
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
Actually, on second thoughts, how could it be modified to send a selected chart to a selected powerpoint presentation?