How to precisely move objects / shapes in Powerpoint in VBA?

In Powerpoint, you may want to align charts and objects on several different slides into the exact same position for consistency. For example, if you had a chart and with text on multiple slides, you would probably want the charts and textbox in the identical location on each slide.

Basics of Moving Objects in Powerpoint

Using the mouse does not give you exact results. Also, its slow and labour intensive. If you have more than a few slides to align, it’s definitely going to take a while and you most likely won’t even do a perfect job. The best solution would be to use a VBA macro to get Powerpoint to perfectly align each object into an exact location. Unfortunately in Powerpoint 2007 and later, the “Record a macro” function no longer exists, so here’s a quick snippet for you to move a selected object into an exact location based on pixel coordinates:

With ActiveWindow.Selection.ShapeRange
  .Left = 50 'change the number for desired x position
  .Top = 50 'change the number for desired y position
End With

Moving Objects to the Center of a Slide Horizontally and Vertically

We will need the height and width of the slide and the object to calculate the correct amount of movement to center an object both horizontally and vertically. This comes in very handy if you have a large number of objects you need to center. Of course, you can build on the solution and customize it so that it moves objects to a formula.

The script basically move object’s top left corner moved to new coordinates:

  • The width (or height) of the slide divided by two less the width (or height) of object divided by two.
  • To simplify the formula, it would be (width of slide minus width of object) / 2. (The same for height.)

To use the code, just select an object and run this macro.

Option Explicit

Public Sub centerObject()
  On Error GoTo Err_Handler
  Dim lSlideHeight As Long, lSlideWidth As Long
  Dim lObjectHeight As Long, lObjectWidth As Long
  Dim X As Long, Y As Long
  Dim shp As Object
  
  If ActiveWindow.Selection.Type = ppSelectionNone Then
    MsgBox "Please select object", vbExclamation, "Make Selection"
  Else
    Set shp = ActiveWindow.Selection.ShapeRange(1)
    
    lSlideHeight = ActivePresentation.PageSetup.SlideHeight 'get slide vertical height
    lSlideWidth = ActivePresentation.PageSetup.SlideWidth 'get slide horizontal width
    
    'object width and height
    lObjectHeight = shp.Height
    lObjectWidth = shp.Width 
    
    X = (lSlideWidth - lObjectWidth) / 2 'calculate horizontal position
    Y = (lSlideHeight - lObjectHeight) / 2 'calculate vertical position
    
    shp.Left = X 'move object horizontal
    shp.Top = Y 'move object vertical
  End If
  
Exit_Label:
  On Error Resume Next
  Set shp = Nothing
  Exit Sub
Err_Handler:
  MsgBox Err.Description, vbCritical
  Resume Exit_Label
End Sub

Resize a Shape in Powerpoint

What if you want to resize an images on each page into the same width, height, or both so your presentation can have a clean and consistent look? Here’s a snippet for that too:

With ActiveWindow.Selection.ShapeRange
  .Height = 400 'change the number for desired height in pixels
  .Width = 400 'change the number for desired width in pixels
End With

Combining the two:

With ActiveWindow.Selection.ShapeRange
  .Height = 400
  .Width = 400
  .Left = 50
  .Top = 50
End With

Other Notes

  • If you only want to center vertically / horizontally, comment out the line with “shp.Left” or “shp.Top” depending on what you do not want to do.

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>