Change the size of a shape in Powerpoint 2007 using VBA


Automation in Powerpoint 2007 is not so easy as it seems, because the macro recording is not available any more. What I wanted to do is to change the shape of some graphs to have a specific size and location.
The following code changes the size and location of the current shape according to our preferences.
Public Sub ChangeImage()
   With ActiveWindow.Selection.ShapeRange(1)
      .LockAspectRatio = msoFalse
      .Width = 720
      .Height = 153.9213
      .Top = 77.8604
      .Left = 0
      ' optional
      '.ZOrder msoSendToBack
   End With
End Sub

Public Sub PrintInfo() Debug.Print vbCrLf & "Public Sub ChangeImage()" Debug.Print vbTab & "With ActiveWindow.Selection.ShapeRange(1)" With ActiveWindow.Selection.ShapeRange(1) Debug.Print vbTab & vbTab & ".LockAspectRatio = msoFalse" Debug.Print vbTab & vbTab & ".Width = " & Str(.Width) Debug.Print vbTab & vbTab & ".Height = " & Str(.Height) Debug.Print vbTab & vbTab & ".Top = " & Str(.Top) Debug.Print vbTab & vbTab & ".Left = " & Str(.Left) End With Debug.Print vbTab & "End With" Debug.Print "End Sub" End Sub

The second sub prints the desired code according to the selected shape, so we do not need to check the shape size and position manually before writing the desired code.

Comments

Anonymous said…
Thanks, this really helped.

It's like Microsoft didn't want us to do anything with VBA and PowerPoint!
tricky01 said…
Great piece of code and I like using debug print to write the code for you, never seen this before.

If you add 'chr(9) &' into the code too though you can get it to print out the indentation too:

Public Sub printinfo()

Debug.Print vbCrLf & "Public Sub changeimage()"
With ActiveWindow.Selection.ShapeRange
Debug.Print Chr(9) & "With ActiveWindow.Selection.ShapeRange"
Debug.Print Chr(9) & Chr(9) & ".Width = " & Str(.Width)
Debug.Print Chr(9) & Chr(9) & ".Height = " & Str(.Height)
Debug.Print Chr(9) & Chr(9) & ".Top = " & Str(.Top)
Debug.Print Chr(9) & Chr(9) & ".Left = " & Str(.Left)
End With
Debug.Print Chr(9) & "End With"
Debug.Print "End Sub"

End Sub
glTaurus said…
You are right, indentation always helps!

Popular posts from this blog

Write Unicode text using VBA

Calling Fortran intrinsic functions from Visual Basic

Dictionary class extensions (CopyTo, Sort) (C#)