views:

360

answers:

2

Hello everybody,

I have a simple question regarding PowerPoint VBA:

Which VBA code should I use to switch between the "active sheet of paper" (I am sorry I don't know how to name it properly), in which I am doing something with the object(s), and the file (or "field", again sorry for my poor terminology) where all the slides are ?

For example, if I want to move a selected object in the "active sheet of paper" I would use this macro:

ActiveWindow.Selection.ShapeRange.IncrementLeft 6#

and if I want to copy the selected slide in the slides file, I would use this code:

ActiveWindow.Selection.Copy ActiveWindow.View.Paste

But how can I connect these two pieces of script? Let's say I want to move an object in the "active sheet of paper", then copy this whole "sheet", then create its twin in the slides field, and then jump into the twin sheet of paper to do something with objects there?

Shortly, how do I switch from "paper" to "slides" and back to "paper" in VBA?

(Again, I am sorry for terrible terminology here, I hope you understand what I mean here.)

Thank you all in advance.

+2  A: 

If you record a macro in PowerPoint and examine the code, you'll see that it uses the Selection object for just about everything. That's sometimes useful (because it means it's more likely that the code will do what you want if you select another object), but for anything more than a very short macro, it's probably better to refer to the objects directly, as in the following code:

Sub Test()

    ' Get the active presentation

    Dim oPresentation As Presentation
    Set oPresentation = ActivePresentation

    ' Get the first slide in the presentation

    Dim oSlide As Slide
    Set oSlide = oPresentation.Slides(1)

    ' Get the first shape on the slide

    Dim oShape As Shape
    Set oShape = oSlide.Shapes(1)

    ' Nudge the shape to the right

    oShape.Left = oShape.Left + 1

    ' Copy the whole slide

    oSlide.Copy

    ' Paste the slide as a new slide at position 2

    Dim oNewSlides As SlideRange
    Set oNewSlides = oPresentation.Slides.Paste(2)

    ' Get a reference to the slide we pasted

    Dim oNewSlide As Slide
    Set oNewSlide = oNewSlides(1)

    ' Get the first shape on the NEW slide

    Dim oNewShape As Shape
    Set oNewShape = oNewSlide.Shapes(1)

    ' Nudge the shape to the right

    oNewShape.Left = oNewShape.Left + 1

End Sub

Note that pretty much every object has a Select method, so if you do want to explicitly select something, you can. In some cases, you may need to change the active window's view type first - so for example, you can't select a shape on a slide while in slide-sorter view.

Gary McGill
+1  A: 

Thank you very much. Just what I needed.

brilliant
Thanks for your reply, but you should mark the answer as correct rather than posting a new answer :-)
Gary McGill
How do I do it? Can I mark answers if I am not registered or I should get registered first?
brilliant