views:

169

answers:

1

I have tried to create this script that would create 10 empty slides, but it doesn't work for some reason:

Sub CreatingSlides()
    Dim oPresentation As Presentation
    Set oPresentation = ActivePresentation
    Dim oSlide As Slide
    Dim oSlides As SlideRange
    Dim oShape As Shape
    Dim slideNumber As Integer
    Dim myindex As Integer

    Set myindex = 1
    ActivePresentation.Slides.add(Index:=myindex, Layout:=ppLayoutBlank).Select
    For myindex = 1 To 10
        myindex = myindex + 1
        ActivePresentation.Slides.add(Index:=myindex, Layout:=ppLayoutBlank).Select
    Next myindex
End Sub

What have I done wrong here? Maybe my loop here is missing something?

+5  A: 

First:

  Set myindex = 1

should be:

  myindex = 1

Set is for object references. Let is for values and is usually implied but you could also use:

  Let myindex = 1

which has the same affect.

Second, loose the line

  myindex = myindex + 1

That's what the For/Next is doing for you. You might have some different behaviour expectations still so try this and we can go from there.

Swanny
WOW!!! Swanny, it works!!!! Thank you!And also thank you for explaining things about "Let" and "FOr/Next". A small question: why does it create 11 slides instead of 10? Is it because before the "For" line I have inserted "ActivePresentation.Slides.add(Index:=myindex, Layout:=ppLayoutBlank).Select" ?
brilliant
You get one slide from the Slides.Add() before the For loop and 10 from within the for loop. If your intention is to just add 10 slides then loose the two lines before the For loop. The For loop will have 10 iterations with myindex having the value 1 to 10.
Swanny