VBA code to adjust image size in PowerPoint 2016 - vba

I am in my first week of learning VBA, and I am looking for a VBA code that will help me resize and reposition pictures pasted into PowerPoint 2016. The desired picture format details are below:
Size
- Height = 3.39"
- Width = 6.67"
- Rotation = 0
- Scale Height = 62%
- Scale Width = 62%
- Aspect Ratio = Locked
- Relative to original picture size = true
Position
- Horizontal position = 0
- Top Left Corner
- Vertical position = 2.06
- Top Left Corner
Any help would be greatly appreciated.

Below is the code that worked for me. Thanks for the support.
Sub ResizeAll()
For Each tSlide In ActiveWindow.Presentation.Slides
tSlide.Select
With tSlide.Shapes.Item(1)
'assume a blank slide with one image added only
.Select
.Height = 72 * 3.39
.Width = 72 * 6.67
'algin middle (Horizontal Center)
.Left = 0
.Top = ActivePresentation.PageSetup.SlideHeight / 3.25
End With
Next
End Sub

Okay, so this macro will adjust the details of every picture within your powerpoint.
Sub AdjustImages()
Dim curSlide As Slide
Dim curShape As Shape
For Each curSlide In ActivePresentation.Slides
For Each curShape In curSlide.Shapes
With curShape
'size:
''1 inch = 72 points
.Height = 72 * 3.39
.Width = 72 * 6.67
.ScaleHeight 0.62, msoTrue
.ScaleWidth 0.62, msoTrue
.LockAspectRatio = msoTrue
'position:
.Rotation = 0
.Left = 0
.Top = 2.06
'Relative to original picture size = true
End With
Next curShape
Next curSlide
End Sub
The only part of your question that I don't really understand is when you mention it being "relative to original picture size = true". I can't seem to find an attribute that matches that.

Related

Change height of an image while maintaining the width ratio in VBA

After cropping the image, I wish to use macro to change the height of all image while maintaining the width to height ratio so that the image does not look weird . Currently, my code change the correct height but it does not maintain the width to height ratio
Sub resizeall()
Dim i As Long
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.LockAspectRatio = msoTrue
.Height = CentimetersToPoints(6.9)
End With
Next i
End With
End Sub
Any advice will be appreciated
I have try
.LockAspectRatio = msoTrue
.Top = Range("B7").Top
.Left = Range("B7").Left
.ShapeRange.LockAspectRatio = msoTrue
.Height = CentimetersToPoints(6.9)
I am a complete beginner , I been googling but it does not seem to work for me. I could resize the image in word by moving the corner of the image while pressing shift but there too many images.
I found this forum https://www.mrexcel.com/board/threads/insert-and-resize-picture-maintaining-aspect-ratio.1010711/ but I don't understand it and can't incorporate it with my current code.
To resize all images to a common height you can use the following:
Sub resizeall()
Dim i As Long
Dim newHeight As Single: newHeight = CentimetersToPoints(6.9)
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.LockAspectRatio = msoTrue
.Width = AspectWidth(.Width, .Height, newHeight)
.Height = newHeight
End With
Next i
End With
End Sub
Public Function AspectWidth(ByVal OrigWidth As Single, ByVal OrigHeight As Single, _
ByVal newHeight As Single) As Single
'Calculates the new width in relation to the supplied new height
'maintaining the aspect ratio of the original width/height
If OrigHeight <> 0 Then
AspectWidth = (OrigWidth / OrigHeight) * newHeight
Else
AspectWidth = 0
End If
End Function

Error in VBA for powerpoint when trying to center a title(424: object required)

With myPresentation.Slides(index).Shapes(1).TextFrame.TextRange.Text
.Left = (ActivePresentation.PageSetup.SlideWidth - .Width) / 2
.Top = (ActivePresentation.PageSetup.SlideHeight - .Height) / 2
End With
So basically I have a slide with a 1 title object and I am trying to format it to be centered,
.Left = (ActivePresentation.PageSetup.SlideWidth - .Width) / 2
but this line throws an object required error. Any help is appreciated
The Shape object has Left, Top, Width, and Height properties. TextFrame, TextRange, and Text are irrelevant in this case.
Option Explicit
Sub CenterTitle()
Dim myPresentation As Presentation: Set myPresentation = ActivePresentation
With myPresentation.Slides(1).Shapes(1)
.Left = (myPresentation.PageSetup.SlideWidth - .Width) / 2
.Top = (myPresentation.PageSetup.SlideHeight - .Height) / 2
End With
End Sub

Resize excel comments to fit text with specific width

I'd like to have the comment box fit the comments just right (no extra space at the bottom).
I know there is the .AutoSize but I want the maximum width to be 300.
Here is the code I have,
For Each mycell In myRng.Cells
If Not (mycell.Comment Is Nothing) Then
With mycell.Comment.Shape
.TextFrame.AutoSize = True
If .width > 300 Then
lArea = .width * .height
.width = 300
.height = (lArea / 300)
End If
End With
End If
Next mycell
mycell and myRng are Range datatypes, lArea is Long.
Now, this works relatively well but leaves extra space at the bottom of a number of comments because the area the AutoSized text takes up is different from the area of the AutoSized comment box.
Is there a way to check for blank space inside a comment and then trim it? Or is what I have the best it is going to be?
try this ... test comment has been placed in cell E4
discovered by putting Range("e4").Comment.Shape.TextFrame in the Watch window
Sub testComment()
With Range("e4").Comment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
.Width = 300
.Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
End Sub
I've changed the code in the previous comment to only resize the box if width is above 300 because otherwise the final size of small boxes were messed up. Also changed to go through all comment box on activesheet
Sub reset_box_size()
Dim pComment As Comment
For Each pComment In Application.ActiveSheet.Comments
With pComment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
'only resize the autosize if width is above 300
If .Width > 300 Then .Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
Next
End Sub
there is an autosizefunction.
here is a small code to show how to use:
Dim Rg_Com as Range , Rg_Value as Range
Set Rg_Com = Cells(1,1)
Set Rg_Value = Cells(1,2)
'Comment in A1 will be same as Text in B1:
With Rg_Com
.ClearComments
.AddComment
With .Comment
.Text Text:=Rg_Value.Value2
.Shape.TextFrame.AutoSize = True '<<< just to make all text visible in one comment, all chars having the basic size
End With
End With

VBA not copying whole chart into PowerPoint

I'm dealing with an issue where my VBA code somehow chooses not to include the whole chart when copying to a powerpoint slide. I have the following code:
This code creates my Doughnut chart from 2 numbers.
Function CreateTwoValuesPie(ByVal X As Long, ByVal Y As Long) As Chart
'Returnerer
Set CreateTwoValuesPie = charts.Add
CreateTwoValuesPie.ChartType = XlChartType.xlDoughnut
With CreateTwoValuesPie.SeriesCollection.NewSeries
.Values = Array(X, Y)
End With
With CreateTwoValuesPie
.ChartArea.Format.Fill.Visible = msoFalse
.ChartArea.Format.Line.Visible = msoFalse
.Legend.Delete
.ChartGroups(1).DoughnutHoleSize = 70
With .SeriesCollection(1)
.Points(1).Format.Fill.ForeColor.RGB = RGB(255, 158, 77) 'Score Orange
.Points(2).Format.Fill.ForeColor.RGB = RGB(175, 171, 170) '10 - Score GrĂ¥
.Format.Line.ForeColor.RGB = RGB(255, 255, 255)
End With
End With
End Function
This code store the different object and numbers:
Set oPPTApp = CreateObject("PowerPoint.Application")
Set oPPTFile = oPPTApp.Presentations.Open(PP)
Set oPPTShape10 = oPPTFile.Slides(1)
d11 = Format(Dashboard.EAScore1.Caption, "Standard")
Set ch1 = CreateTwoValuesPie(d11, 10 - d11)
ch1.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
With oPPTShape10.Shapes.Paste
.Top = 127
.Width = 177
.Left = 393
End With
The code works fine and creates the correct chart from the number (d11, 10-d11) but when I copy the figure and insert it into my powerpoint slide oPPTShape10 it only copy part of the chart.
This can be seen in the image below:
The correct should've look like the one in the image below:
It worked some days ago and I haven't changed anything since then? Does anyone know how I can make it show the whole figure instead of only the topleft corner of it?

code for batch crop picture in powerpoint

I wrote a macro to batch crop and resize the pictures in selected slides, the resize work good, but after I added the crop codes, it is not working. Please see the codes as below, is there any thing wrong? welcome and thanks suggestion.
Sub crpicture ()
Dim osld As Slide
Dim oshp As Shape
For Each osld In ActiveWindow.Selection.SlideRange
For Each oshp In osld.Shapes
oshp.PictureFormat.CropLeft = 0
oshp.PictureFormat.CropTop = 0.5 * 72
oshp.PictureFormat.CropRight = oshp.Width - oshp.Height
oshp.PictureFormat.CropBottom = 0
oshp.Height = 3 * 72
oshp.Left = 3.4 * 72
oshp.Top = 0.7 * 72
oshp.ZOrder msoSendToBack
Next
Next
End Sub