Hello,
Every month I have to manually copy charts from Excel and paste them into Power Point. It is a manual process and I would like to improve it by adding a VBA code. Problem 1: The code below allows me to copy one Excel chart into Power Point each time. I have 3 charts on one tab and i want to copy and paste all 3 at the same time to PPT. Now, i have to select one chart and run the code below. Then select another chart on the same tab and run the code and so on.
Problem 2: When i run the VBA code below, i get an extra blank sheet in my power point presentation and i would like to remove that part of the code.
Please let me know if you need my excel file and ppt file. This is my first post and i dont see an option for uploading files.
Thank you,
Miro
Sub ChartToPresentation()
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim AddSlidesToEnd As Boolean
AddSlidesToEnd = False
' Make sure a chart is selected
If ActiveChart Is Nothing Then
MsgBox "Please select a chart and try again.", vbExclamation, _
"No Chart Selected"
Else
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Reference active slide
Set PPSlide = PPPres.Slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
' Copy chart
ActiveChart.ChartArea.Copy
' Paste chart
PPSlide.Shapes.Paste.Select
' Position pasted chart
' This is the keypoint
' I want to replace this with the selection of appropriate layout
' and placeholder in that layout
PPApp.ActiveWindow.Selection.ShapeRange.Left = 29.52
PPApp.ActiveWindow.Selection.ShapeRange.Top = 79.2
PPApp.ActiveWindow.Selection.ShapeRange.Width = 216
PPApp.ActiveWindow.Selection.ShapeRange.Height = 121.68
If PPApp.ActivePresentation.Slides.Count = 0 Then
' Other key point
' can I add a specific layout, for example one named Two Content Layout + takeout
Set PPSlide = PPApp.ActivePresentation.Slides.Add(1, ppLayoutBlank)
Else
If AddSlidesToEnd Then
'Appends slides to end of presentation and makes last slide active
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
Set PPSlide = PPApp.ActivePresentation.Slides(PPApp.ActivePresentation.Slides.Count)
Else
'Sets current slide to active slide
Set PPSlide = PPApp.ActiveWindow.View.Slide
End If
End If
'Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End If
End Sub
Every month I have to manually copy charts from Excel and paste them into Power Point. It is a manual process and I would like to improve it by adding a VBA code. Problem 1: The code below allows me to copy one Excel chart into Power Point each time. I have 3 charts on one tab and i want to copy and paste all 3 at the same time to PPT. Now, i have to select one chart and run the code below. Then select another chart on the same tab and run the code and so on.
Problem 2: When i run the VBA code below, i get an extra blank sheet in my power point presentation and i would like to remove that part of the code.
Please let me know if you need my excel file and ppt file. This is my first post and i dont see an option for uploading files.
Thank you,
Miro
Sub ChartToPresentation()
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim AddSlidesToEnd As Boolean
AddSlidesToEnd = False
' Make sure a chart is selected
If ActiveChart Is Nothing Then
MsgBox "Please select a chart and try again.", vbExclamation, _
"No Chart Selected"
Else
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Reference active slide
Set PPSlide = PPPres.Slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
' Copy chart
ActiveChart.ChartArea.Copy
' Paste chart
PPSlide.Shapes.Paste.Select
' Position pasted chart
' This is the keypoint
' I want to replace this with the selection of appropriate layout
' and placeholder in that layout
PPApp.ActiveWindow.Selection.ShapeRange.Left = 29.52
PPApp.ActiveWindow.Selection.ShapeRange.Top = 79.2
PPApp.ActiveWindow.Selection.ShapeRange.Width = 216
PPApp.ActiveWindow.Selection.ShapeRange.Height = 121.68
If PPApp.ActivePresentation.Slides.Count = 0 Then
' Other key point
' can I add a specific layout, for example one named Two Content Layout + takeout
Set PPSlide = PPApp.ActivePresentation.Slides.Add(1, ppLayoutBlank)
Else
If AddSlidesToEnd Then
'Appends slides to end of presentation and makes last slide active
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
Set PPSlide = PPApp.ActivePresentation.Slides(PPApp.ActivePresentation.Slides.Count)
Else
'Sets current slide to active slide
Set PPSlide = PPApp.ActiveWindow.View.Slide
End If
End If
'Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End If
End Sub