How to Set Powerpoint Object to Width or Height by Inches in VBA?

The below code changes the width and height of any object in Powerpoint to exactly 5 inches without changing the aspect ratio. The aspect ratio is the ratio of height and width. Keeping the same aspect ratio prevents an image from looking stretched. But before you run this code/macro, you first need to select the object for which you want to change width else it will alert you to select the object.

In VBA, height and width properties are specified in points as units. (So if shp.Height = 1 means height is set as 1 point.)

One inch is equal to 72 points. So if you want to change the width to 5 inches, you would set the width property to 360 = 5 * 72. To keep the aspect ratio the same, we would multiple height by the same height-to-width ratios.

Public Sub changeObjectSizeInch()
  On Error GoTo Err_Handler
  Dim shp As Shape
  Dim lWidth As Long, lHeight As Long
  
  If ActiveWindow.Selection.Type = ppSelectionNone Then
    MsgBox "Please select embedded OLE object", vbExclamation, "Make Selection"
  Else
    Set shp = ActiveWindow.Selection.ShapeRange(1)
    
    lHeight = shp.Height
    lWidth = shp.Width
      
    shp.Height = 5 * 72 * lHeight / lWidth  
    shp.Width = 5 * 72  
  End If
  
Exit_Label:
  On Error Resume Next
  Set shp = Nothing
  Exit Sub
Err_Handler:
  MsgBox Err.Description, vbCritical
  Resume Exit_Label
End Sub


To change the # of inches, replace the “5” in both lines to the # of inches you want.

    shp.Height = [# of inches] * 72 * lHeight / lWidth  
    shp.Width = [# of inches] * 72  

To set the height to 5 inches and adjust width to the same aspect ratio, replace the code above with this.

    shp.Height = 5 * 72 
    shp.Width = 5 * 72 * lWidth / lHeight

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>