【问题标题】:How to make a loop in PowerPoint VBA?如何在 PowerPoint VBA 中制作循环?
【发布时间】:2010-12-15 02:44:53
【问题描述】:

据我所知,下面的代码从活动窗口中获取了一个形状,稍微微调它,复制幻灯片并将其粘贴到当前幻灯片之后,然后将粘贴的幻灯片变成活动窗口,然后微调它再次:

子测试()

' 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

结束子

据我所知,为了实现这段代码,我应该打开一个活动窗口,并且它应该至少有一个形状。在我运行这段代码之前,我只有一张幻灯片;代码运行后,我有两张幻灯片:旧的一张是 1 号,新的一张是 2 号。

如果我再次运行此代码,我将得到三张幻灯片:最旧的仍然是 1 号,但最旧的一张是 2 号,而不是 3 号。

我的问题是如何让它生成幻灯片,以便新的幻灯片总是具有更大序号的幻灯片,即每张新创建的幻灯片都应该是幻灯片预览侧栏中的最后一张(最低的一张)?

还有,我怎样才能让它变成一个循环?这样我就不需要一次又一次地重新运行这段代码,而只需使用给定的循环迭代次数创建一个循环。

我猜,如果它应该是一个循环,那么幻灯片索引应该变成一个变量,但我不知道如何在 PowerPoint VBA 中做到这一点。

【问题讨论】:

    标签: vba loops iteration powerpoint


    【解决方案1】:

    我不确定您的代码是否有意义。它本质上是:

    1. 获取第一张幻灯片
    2. 获取幻灯片上的第一个形状
    3. 向右移动 1 个单位
    4. 复制第一张幻灯片
    5. 作为第二张幻灯片粘贴
    6. 在新的 2nd 上获取 1st 形状 幻灯片
    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 次。

    【讨论】:

    • 谢谢你,马克。正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    相关资源
    最近更新 更多