Powerpoint VBA选择一系列幻灯片

时间:2017-12-14 22:36:04

标签: powerpoint-vba

虽然如果您知道索引/名称,有很多关于如何设置范围的响应,但我需要的是一种选择一系列幻灯片而不指定每个幻灯片索引或名称的方法。

我在一个部分的开头有一个名为“C2 Title”的幻灯片,在一个部分的末尾有一个名为“C2Approval”的幻灯片。两者之间的幻灯片数量将根据用户是否在中间添加幻灯片而有所不同。我想选择两者之间的所有幻灯片,无论插入多少张幻灯片。

我怀疑有一种方法可以遍历索引来填充数组,但我似乎无法弄清楚如何做到这一点。

更新:下面的每个请求都是我尝试过的

Sub SelectSection()
Dim sIndex As Long
Dim eIndex As Long
Dim sArray() As Long
Dim sSlides As SlideRange


ActivePresentation.Slides("C2Title").Select
sIndex = ActiveWindow.Selection.SlideRange.SlideIndex

ActivePresentation.Slides("C2DirectionalApproval").Select
eIndex = ActiveWindow.Selection.SlideRange.SlideIndex

'This solution only gets the first/last slide of range
Set sSlides = ActivePresentation.Slides.Range(Array(eIndex - 1, sIndex + 1))

'Problem is this assumes I've already selected Slides
sSlides = ActiveWindow.Selection.SlideRange
 ReDim myArray(1 To sSlides.count)
      For y = LBound(sArray) To UBound(myArray)
        sArray(y) = Slides(y).SlideIndex
      Next y



End Sub

1 个答案:

答案 0 :(得分:1)

感谢Steve Rindsberg这是个好主意并让它发挥作用

Sub SelectSection()
Dim eIndex As Long 'Index of End of Selection of Slide
Dim lIndex As Long 'Index used to select slides in loop
Dim pIndex As Long 'Index used to determine paste spot

lIndex = ActivePresentation.Slides("C2Title").SlideIndex + 1
eIndex = ActivePresentation.Slides("C2Approval").SlideIndex
pIndex = ActivePresentation.Slides("C3Title").SlideIndex


ActivePresentation.Slides(lIndex).Select
Do While (ActiveWindow.Selection.SlideRange.SlideIndex < eIndex)
'Copies the selected slide
ActivePresentation.Slides(lIndex).Copy
'Selects next slide in Chapter 3, pastes in copied slide and changes subtitle
ActivePresentation.Slides(pIndex).Select
pIndex = pIndex + 1
ActivePresentation.Slides.Paste Index:=pIndex
ActivePresentation.Slides(pIndex).Shapes("Subtitle").TextFrame.TextRange.Text = "Chapter 3: Information Systems Architecture"
'Selects next slide in presentation
ActivePresentation.Slides(lIndex).Select
lIndex = lIndex + 1
'Selects slide to copy
ActivePresentation.Slides(lIndex).Select
Loop

End Sub