Hello,
I'm trying to write a VBA code to replace an old graph in PowerPoint with a new graph from Excel. I can currently export the graph to the PowerPoint file but I cannot get the positioning right.
I've tried using the functions .Top, .Height, .Width etc. but this doesn't put the graph where I want it.
Since I cannot get the positioning right using the functions mentioned above, I'm wondering if there is a way I can measure the position and dimensions of the old graph and then paste the Excel graph using the position and dimensions. Is this possible to do?
I'm trying to write a VBA code to replace an old graph in PowerPoint with a new graph from Excel. I can currently export the graph to the PowerPoint file but I cannot get the positioning right.
I've tried using the functions .Top, .Height, .Width etc. but this doesn't put the graph where I want it.
Since I cannot get the positioning right using the functions mentioned above, I'm wondering if there is a way I can measure the position and dimensions of the old graph and then paste the Excel graph using the position and dimensions. Is this possible to do?
VBA Code:
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Worksheets("Sheet1").Activate
ActiveSheet.ChartObjects("Graph1").Chart.CopyPicture
If PowerPointApp Is Nothing Then _
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
Application.ScreenUpdating = False
Set myPresentation = PowerPointApp.Presentations.Open(Filename:="C:\x\xy\xyz\x\xx.pptx")
Set mySlide = myPresentation.Slides(4)
ActiveChart.ChartArea.Copy
mySlide.Shapes.Paste
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
' Position pasted chart
With myShape
.Top = x
.Height = x
.Width = x
.ZOrder (msoSendToBack)
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub