How to make a loop in PowerPoint VBA?

As far as I know, the code below gets the form from the active window, pushes it a bit, copies the slide and pastes it immediately after the current one, then turns the inserted slide into the active window and pushes 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 understand, in order to implement this code, I have to open an active window and have at least one figure in it. Before I run this code, I have only one slide; after executing the code, I have two slides: the older one is number 1, and the new one is number 2.

If I run this code again, I will get three slides: the oldest of them is still number 1, but the oldest of them is number 2, not number 3.

, , , .. ( )?

, ? , .

, , , , PowerPoint VBA.

+3
1

, . :

  • 1-
  • 1 .
  • 1- 2-
  • 1 .

, ?

:

,

Set oNewSlides = oPresentation.Slides.Paste(2)

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

, :

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

, , , , , , , .. 10 .

+4

Source: https://habr.com/ru/post/1722473/


All Articles