如何在 PowerPoint VBA 中进行循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1709154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a loop in PowerPoint VBA?
提问by brilliant
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
子测试()
' 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
结束子
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.
据我所知,为了实现这段代码,我应该打开一个活动窗口,其中至少应该有一个形状。在我运行这段代码之前,我只有一张幻灯片;代码运行后,我有两张幻灯片:旧的一张是 1 号,新的一张是 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.
如果我再运行一次此代码,结果将得到三张幻灯片:最旧的一张仍然是 1 号,但最旧的一张是 2 号,而不是 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.
我想,如果它应该是一个循环,那么幻灯片索引应该变成一个变量,但我不知道如何在 PowerPoint VBA 中做到这一点。
回答by Mark
I'm not sure your code makes any sense. It essentially:
我不确定您的代码是否有意义。它本质上:
- Gets the 1st slide
- Gets the 1st shape on the slide
- Moves it 1 unit to the right
- Copies the 1st slide
- Pastes it in as the 2nd slide
- Gets the 1st shape on the new 2nd slide
- Moves it 1 unit to the right
- 获取第一张幻灯片
- 获取幻灯片上的第一个形状
- 将其向右移动 1 个单位
- 复制第一张幻灯片
- 将其粘贴为第二张幻灯片
- 获取新的第二张幻灯片上的第一个形状
- 将其向右移动 1 个单位
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.
这需要最后一张幻灯片,复制它,将副本粘贴为新的最后一张,向右轻推第一个形状,获取新的最后一张幻灯片,复制它,将副本粘贴为最后一张,将第一个形状向右轻推,等等......它会这样做10次。