如何在PowerPoint VBA中创建循环?

3
据我所知,以下代码从活动窗口获取一个形状,稍微移动一下,复制幻灯片并将其粘贴到当前幻灯片的右侧,然后将粘贴的幻灯片转换为活动窗口,并再次移动它:

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

据我理解,为了实现这段代码,我应该打开一个活动窗口,并且至少要有一个形状。在运行这段代码之前,我只有一页幻灯片;运行代码后,我就有了两页幻灯片:老的是1号,新的是2号。
如果我再运行一次这段代码,我会得到三页幻灯片作为结果:最老的仍然是1号,但最新的是2号,不是3号。
我的问题是如何使其产生幻灯片,以便较新的幻灯片始终具有更大的序号,即每个新创建的幻灯片都应该是幻灯片预览侧栏中的最后一个(最低)?
还有,如何将其变成一个循环?这样我就不需要一遍又一遍地重新运行这段代码,而只需用给定数量的循环迭代次数创建循环即可。
我想,如果它应该是一个循环,那么幻灯片索引应该被转换为一个变量,但我不知道如何在PowerPoint VBA中实现它。
1个回答

4
我不确定你的代码是否有任何意义。它基本上:
  1. 获取第一张幻灯片
  2. 获取幻灯片上的第一个形状
  3. 将其向右移动1个单位
  4. 复制第一张幻灯片
  5. 将其粘贴为第二张幻灯片
  6. 获取新第二张幻灯片上的第一个形状
  7. 将其向右移动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次。


谢谢,马克。正是我想要的。 - brilliant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接