Excel VBA to Powerpoint - paste special chart object link

FinUser

New Member
Joined
Aug 10, 2023
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi



I have this code below from @ John Peltier and it works great. However I want to change 2 things - can anyone help?



First: paste the charts in linked to the originals in excel so I can update them automatically. In other words what you get if you: Copy, Paste Special, Paste Link, “Microsoft Excel chart Object”.



Second: specify their position on the slide (ie, horizontal 2.04 and vertical 4.82)



Help hugely appreciated



Many thanks



style='font-family:"Arial",sans-serif'>Sub ChartsToPresentation()
' 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 PresentationFileName As Variant
Dim SlideCount As Long
Dim iCht As Integer

' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide

For iCht = 1 To ActiveSheet.ChartObjects.Count
' copy chart as a picture
ActiveSheet.ChartObjects(iCht).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture

' Add a new slide and paste in the chart
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
With PPSlide
' paste and select the chart picture
.Shapes.Paste.Select
' align the chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
End With

Next

' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
When you say that you want to specify the horizontal and vertical position, I"m not sure whether you mean the left and top positions or the width and height. So I've assumed you want the left and top positions. If this is not what you meant, replace .Left and .Top with .Width and .Height.

VBA Code:
' copy the chart
ActiveSheet.ChartObjects(iCht).Copy

' add a new slide
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)

' paste and position linked chart
With PPSlide.Shapes.PasteSpecial(DataType:=ppPasteShape, Link:=msoTrue)(1)
    .Left = 2.04
    .Top = 4.82
End With

' go to the new slide (optional)
PPSlide.Select

Hope this helps!
 
Upvote 0
When you say that you want to specify the horizontal and vertical position, I"m not sure whether you mean the left and top positions or the width and height. So I've assumed you want the left and top positions. If this is not what you meant, replace .Left and .Top with .Width and .Height.

VBA Code:
' copy the chart
ActiveSheet.ChartObjects(iCht).Copy

' add a new slide
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)

' paste and position linked chart
With PPSlide.Shapes.PasteSpecial(DataType:=ppPasteShape, Link:=msoTrue)(1)
    .Left = 2.04
    .Top = 4.82
End With

' go to the new slide (optional)
PPSlide.Select

Hope this helps!
This is great, thanks so much. When I try and adjust the original code for it (as per below), I get issues with this line:

With PPSlide.Shapes.PasteSpecial(DataType:=ppPasteShape, Link:=msoTrue)(1)

Apologies for my ignorance, would you mind taking a look at it? thank you ever so much...

Sub ChartsToPresentation()
' 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 PresentationFileName As Variant
Dim SlideCount As Long
Dim iCht As Integer


' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide

For iCht = 1 To ActiveSheet.ChartObjects.count

' copy the chart
ActiveSheet.ChartObjects(iCht).Copy

' add a new slide
SlideCount = PPPres.Slides.count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)

' paste and position linked chart
With PPSlide.Shapes.PasteSpecial(DataType:=ppPasteShape, Link:=msoTrue)(1)
.Left = 2.04
.Top = 4.82
End With

Next

' go to the new slide (optional)
PPSlide.Select
End Sub
 
Upvote 0
Sorry, my mistake. Leave that line as is, but replace...

VBA Code:
ActiveSheet.ChartObjects(iCht).Copy

with

VBA Code:
ActiveSheet.ChartObjects(iCht).Chart.ChartArea.Copy

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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