views:

154

answers:

1

In VBA for PowerPoint, as far as I understand, this code gets only the first shape in the active window and nudges it:

Set oShape = oSlide.Shapes(1)

oShape.Left = oShape.Left + 5

And if I wanted to nudge all the shapes, I would use a loop for this.

But how can I get and nudge only certain shapes, based on their number?

For example, let's say I have only 3 shapes in the active window. What if I want to nudge shape 1 and shape 3, but I don't want to touch shape 2. How could I do that?

+3  A: 

If you want to specify specific shapes by number, use something like this:

For Each shapeNum In Array(1, 3, 5, 9, 10)
    Set oShape = oSlide.Shapes(shapeNum)

    oShape.Left = oShape.Left + 5
Next shapeNum

If you just want to randomly move certain shapes, then use this:

For shapeNum = 1 To oSlide.Shapes.Count
    If Rnd < 0.5 Then ''1 in 2 chance
        Set oShape = oSlide.Shapes(shapeNum)

        oShape.Left = oShape.Left + 5
    End If
Next shapeNum

If you wanted something else, add the detail to your question.

Artelius
WOW!!!Thank you, Artelius! The upper part of your answer is exactly what I wanted to find out.
brilliant
Thanks also for editing, Artelius, but don't you think the word "loop" must be retained in the title? Many noobs like me know the word loop, but they may not know what it means to iterate. I myself came to know that to iterate means to use a loop only 3 days ago!
brilliant
Good point, seeing as this is a VBA question. The problem is kind of hard to describe. But I think you're right about "iterate".
Artelius