如何在 PowerPoint VBA 中制作循环?

发布于 2024-08-11 03:19:08 字数 1086 浏览 1 评论 0原文

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

子测试()

' 获取活动演示文稿

昏暗的演示文稿作为演示文稿
设置 oPresentation = ActivePresentation

' 获取演示文稿中的第一张幻灯片

将 oSlide 调暗为幻灯片
设置 oSlide = oPresentation.Slides(1)

' 获取幻灯片上的第一个形状

变暗 oShape 作为形状
设置 oShape = oSlide.Shapes(1)

' 将形状向右微移

oShape.Left = oShape.Left + 1

' 复制整张幻灯片

o幻灯片复制

' 将幻灯片作为新幻灯片粘贴到位置 2

将 oNewSlides 调暗为 SlideRange
设置 oNewSlides = oPresentation.Slides.Paste(2)

' 获取对我们粘贴的幻灯片的引用

将 oNewSlide 变暗为幻灯片
设置 oNewSlide = oNewSlides(1)

' 获取新幻灯片上的第一个形状

将 oNewShape 变暗为形状
设置 oNewShape = oNewSlide.Shapes(1)

' 将形状向右微移

oNewShape.Left = oNewShape.Left + 1

结束子

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

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

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

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

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

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-08-18 03:19:08

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文