sifar786
04-17-2020, 01:19 PM
Is there a way to resize shapes(pictures,tables,charts) pasted from an Excel Worksheet to Powerpoint slides, so that each shape is resized relative to it's original dimensions (LockAspectRatio=True) and placed exactly between a Slide's SubTitle and Footer and centered horizontally?
The Shapes may be of varying sizes i.e. sometimes very small or sometimes greater than the dimensions of the Slide. Therefore the criteria is to resize them (without distorting) to fit nicely between the Subtitle and footer and horizontally center-aligned.
Is this possible using Excel VBA?
Below logic is not working correctly.
Sub ResizeShapesInSlide()
Dim L#, T#, W#, H#, SlideCnt%, i%
Dim sHeight#, sWidth#
Dim oSlide As Slide, oShp As Shape
SlideCnt = ActivePresentation.Slides.Count
For i = 1 To SlideCnt
Set oSlide = ActivePresentation.Slides(i)
With oSlide
.Select
' get Slide height & width
sHeight = ActivePresentation.PageSetup.SlideHeight
sWidth = ActivePresentation.PageSetup.SlideWidth
Set oShp = .Shapes("Picture 1")
With oShp
.Select
.LockAspectRatio = msoTrue
' resize if image larger than Slide dimensions
If .Width >= sWidth Then .Width = sWidth - oSlide.Shapes("SubTitle").Left
If .Height >= sHeight Then .Height = sHeight - oSlide.Shapes("Footer").Top
' L = .Left: T = .Top: W = .Width: H = .Height
' Set shape Top to SubTitle Top + Height
.Top = (oSlide.Shapes("SubTitle").Top + oSlide.Shapes("SubTitle").Height) + 5
' Initially set Shape to SubTitle Left
.Left = oSlide.Shapes("SubTitle").Left
' now adjust Shape height till Footer Top
.Height = .Height - (sHeight - oSlide.Shapes("Footer").Top - 5)
' finally center shape horizontally
.Left = (sWidth - .Width) / 2
End With
End With
Next i
End Sub
The Shapes may be of varying sizes i.e. sometimes very small or sometimes greater than the dimensions of the Slide. Therefore the criteria is to resize them (without distorting) to fit nicely between the Subtitle and footer and horizontally center-aligned.
Is this possible using Excel VBA?
Below logic is not working correctly.
Sub ResizeShapesInSlide()
Dim L#, T#, W#, H#, SlideCnt%, i%
Dim sHeight#, sWidth#
Dim oSlide As Slide, oShp As Shape
SlideCnt = ActivePresentation.Slides.Count
For i = 1 To SlideCnt
Set oSlide = ActivePresentation.Slides(i)
With oSlide
.Select
' get Slide height & width
sHeight = ActivePresentation.PageSetup.SlideHeight
sWidth = ActivePresentation.PageSetup.SlideWidth
Set oShp = .Shapes("Picture 1")
With oShp
.Select
.LockAspectRatio = msoTrue
' resize if image larger than Slide dimensions
If .Width >= sWidth Then .Width = sWidth - oSlide.Shapes("SubTitle").Left
If .Height >= sHeight Then .Height = sHeight - oSlide.Shapes("Footer").Top
' L = .Left: T = .Top: W = .Width: H = .Height
' Set shape Top to SubTitle Top + Height
.Top = (oSlide.Shapes("SubTitle").Top + oSlide.Shapes("SubTitle").Height) + 5
' Initially set Shape to SubTitle Left
.Left = oSlide.Shapes("SubTitle").Left
' now adjust Shape height till Footer Top
.Height = .Height - (sHeight - oSlide.Shapes("Footer").Top - 5)
' finally center shape horizontally
.Left = (sWidth - .Width) / 2
End With
End With
Next i
End Sub