Loop through all pictures in a presentation with VBA

J

Jesper Kaas

Using Powerpoint 2003.

I have a presentation with 196 slides, where most slides have a
picture inserted from a file.
All these pictures must be resized, and the origin changed to the same
values for all pictures.
A recorded macro does this for a single slide, so I could manually go
through all slides and run the macro.

How do you make a loop that goes through all pictures in a
presentation ?
I tried the following:

Dim Pict as Object
For each Pict in Activedocument.Shapes
{code to reformat picture}
Next Pict

but get error "Object required" on the second line.

I am familiar with Object model and VBA for Word and Excel, but never
tried VBA in Powerpoint before.
 
D

David M. Marcovitz

I think you want something like this, which cycles through each shape
within each slide, checks to see if it is a picture, and then adjusts the
size and location. You will, obviously, adjust the stuff that changes
the size and location.

Sub ResizePicture()
Dim shp As Shape
Dim sld As Slide

For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.Type = msoPicture Then
shp.Left = 25
shp.Top = 25
shp.Height = 100
shp.Width = 50
End If
Next
Next
End Sub


--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jesper Kaas

I think you want something like this, which cycles through each shape
within each slide, checks to see if it is a picture, and then adjusts the
size and location. You will, obviously, adjust the stuff that changes
the size and location.
Thanks a million. That code looks perfect.
I will try it out tomorrow at work.
 
S

Shyam Pillai

In addition to David's code, check if the Aspect ratio for the image is
locked and turn it off if you intend to change the width as well as the
height of the image.

If the Aspect ratio of the picture is locked then on the line 'shp.Height =
100' will resize the image height to 100 points and resize the image width
to a corresponding value to maintain the aspect ratio. Then the next line
'shp.Width = 50' will resize the image width to 50 points and immediately
change the image height correspondingly. So the image height will no longer
be 100. It will be a value set by PowerPoint to accommodate the new image
width of 50 points.

To ensure that both height and width are fixed values that you want to set,
turn the LockAspectRatio property to false first.
shp.LockAspectRatio = False
shp.Height = 100
shp.Width = 50
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top