Change height of an image while maintaining the width ratio in VBA - 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

Related

Simultaneous many-shape rotation between fixed positions on Powerpoint

I have six objects, all in a given fixed position, as depicted below
The text boxes all have the same size. I would like to automate the counterclockwise rotation of all text boxes, so that when I use the macro, it will rotate the text 60ยบ ccw (thus BETA becomes ALPHA, ALPHA becomes ZETA and so forth). However, I'm completely clueless on how to write it in VBA! I know that I can set the textbox using
Set myDocument = ActivePresentation.Slides(1)
myDocument.Shapes.AddTextbox(Type:=msoTextOrientationHorizontal, _
Left:=400, Top:=100, Width:=160, Height:=30).TextFrame _
.TextRange.Text = "ALPHA"
But, I'm clueless on how to rotate them. Another alternative would be to create these six TextBoxes and create a function that only changed the text variable, but my VBA knowledge is very elementary, and I wouldn't even know where to begin :\
Can anyone be so kind as to give me a small help?
If you mean to rotate their position and not their orientation it could look like this:
Option Explicit
Public Sub ExampleRotatePositions()
Dim myDocument As Slide
Set myDocument = ActivePresentation.Slides(1)
Dim TextBox(1 To 6) As Shape
'create the textboxes in your desired position.
Set TextBox(1) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=160, Height:=30)
TextBox(1).TextFrame.TextRange.Text = "ALPHA"
Set TextBox(2) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=200, Top:=100, Width:=160, Height:=30)
TextBox(2).TextFrame.TextRange.Text = "BETA"
Set TextBox(3) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=300, Top:=100, Width:=160, Height:=30)
TextBox(3).TextFrame.TextRange.Text = "GAMMA"
Set TextBox(4) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=400, Top:=100, Width:=160, Height:=30)
TextBox(4).TextFrame.TextRange.Text = "DELTA"
Set TextBox(5) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=500, Top:=100, Width:=160, Height:=30)
TextBox(5).TextFrame.TextRange.Text = "EPSILON"
Set TextBox(6) = myDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=600, Top:=100, Width:=160, Height:=30)
TextBox(6).TextFrame.TextRange.Text = "ZETA"
MsgBox "Start rotating now"
'remember last position
Dim LastLeft As Single
LastLeft = TextBox(UBound(TextBox)).Left
Dim LastTop As Single
LastTop = TextBox(UBound(TextBox)).Top
'rotate position
Dim iTextBox As Long
For iTextBox = UBound(TextBox) - 1 To LBound(TextBox) Step -1
TextBox(iTextBox + 1).Left = TextBox(iTextBox).Left
TextBox(iTextBox + 1).Top = TextBox(iTextBox).Top
Next iTextBox
'move first to last position
TextBox(LBound(TextBox)).Left = LastLeft
TextBox(LBound(TextBox)).Top = LastTop
End Sub
Group them using the ShapeRange.Group method and then rotate the group:
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes
.AddShape(msoShapeCan, 50, 10, 100, 200).Name = "shpOne"
.AddShape(msoShapeCube, 150, 250, 100, 200).Name = "shpTwo"
With .Range(Array("shpOne", "shpTwo")).Group
.Fill.PresetTextured msoTextureBlueTissuePaper
.Rotation = 45
.ZOrder msoSendToBack
End With
End With

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

VBA code to adjust image size in PowerPoint 2016

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.

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

Replicate an object in powerpoint using vba?

I want to replicate an selected object in powerpoint using VBA code. I have a following code mention below
Sub CopySizeAndPosition()
' Usage: Select two shapes. The size and position of
' the first shape selected will be copied to the second.
Dim w As Double
Dim h As Double
Dim l As Double
Dim t As Double
With ActiveWindow.Selection.ShapeRange(1)
w = .Width
h = .Height
l = .Left
t = .Top
End With
With ActiveWindow.Selection.ShapeRange(2)
.Width = w
.Height = h
.Left = l
.Top = t
End With
End Sub
But I want to specify my value instead of getting object value. So, please help and thanx in advance!
Assuming you have selected a single shape, you can set your values like this:
' Sets the size and position of the first shape in a selection
Sub SetShapeSizeAndPosition()
With ActiveWindow.Selection.ShapeRange(1)
.Width = 100
.Height = 100
.Left = 100
.Top = 100
End With
End Sub