开发者

How to make a loop in PowerPoint VBA?

开发者 https://www.devze.com 2022-12-10 20:59 出处:网络
As far as I know, the code below gets a shape from the active window, nudges it a bit, copies the slide and pastes it right after the current one, then turns the pasted slide into an active window, an

As far as I know, the code below gets a shape from the active window, nudges it a bit, copies the slide and pastes it right after the current one, then turns the pasted slide into an active window, and nudges it again:

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

As far as I can understand, in order to implement this code, I should have an active window opened and it should have at least one shape in it. Before I run this code I have only one slide; after the code has been run, I have two slides: the older one is number 1, and the newer one is number 2.

If I run this code one more time, I will get three slides as a result: the oldest one being still number 1, but the oldest one being number 2, not number 3.

My question is how can I make it produce slides, so that the newer slides are always the ones with a greater ordinal number, i.e. every newly created slide should be the last one in the slide preview sidebar (the lowest one)?

And also, how can I make it into a loop? So that I don't need to re-run this code again and again, but simply make a loop with a given number开发者_StackOverflow社区 of loop's iterations.

I guess, if it should be a loop, then slides index should be turned into a variable, but I don't know how to do it in PowerPoint VBA.


I'm not sure your code makes any sense. It essentially:

  1. Gets the 1st slide
  2. Gets the 1st shape on the slide
  3. Moves it 1 unit to the right
  4. Copies the 1st slide
  5. Pastes it in as the 2nd slide
  6. Gets the 1st shape on the new 2nd slide
  7. Moves it 1 unit to the right

Why is it moving it twice, once on the original and once on the copy?

Regardless to answer your specific questions:

To paste it as the last slide, replace

Set oNewSlides = oPresentation.Slides.Paste(2)

With

Set oNewSlides = oPresentation.Slides.Paste() #no index pastes as last

To loop use something like this:

Dim oPresentation As Presentation
Set oPresentation = ActivePresentation

Dim oSlide As Slide
Dim oSlides As SlideRange
Dim oShape As Shape
Dim slideNumber As Integer

For slideNumber = 1 To 10

    Set oSlide = oPresentation.Slides(oPresentation.Slides.Count)
    oSlide.Copy
    Set oNewSlides = oPresentation.Slides.Paste()
    Set oSlide = oNewSlides(1)
    Set oShape = oSlide.Shapes(1)
    oShape.Left = oShape.Left + 5

Next slideNumber

This takes the last slide, copies it, pastes the copy as the new last one, nudges the first shape to the right, takes new last slide, copies it, pastes the copy as the last one, nudges the first shape to the right, etc.... It'll do this 10 times.

0

精彩评论

暂无评论...
验证码 换一张
取 消