且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 VBA 将 powerpoint 部分复制到新的演示文稿

更新时间:2023-02-14 23:42:06

如果要复制幻灯片及其部分,则不能仅通过 newPresentation.Slides.Paste 粘贴幻灯片,因为将最后一张幻灯片的部分移动到新粘贴的幻灯片.

If you want to copy slides with their sections, then you can not paste the slide by newPresentation.Slides.Paste only, as that moves the section of the last slide to the newly pasted slide.

这是一个示例,如何逐张复制幻灯片,检查幻灯片是否是节的开头,然后如何添加新节:

Here's an example how to copy slide-by-slide, check if a slide is the beginning of a section, and how to add a new section then:

Public Sub CopySlidesWithSections()
    Dim oldPresentation As Presentation, newPresentation As Presentation
    Dim oldSlide As Slide, newSlide As Slide
    Dim oldSectionProperties As SectionProperties, newSectionProperties As SectionProperties
    Dim i As Integer

    Set oldPresentation = ActivePresentation
    Set oldSectionProperties = oldPresentation.SectionProperties

    Set newPresentation = Application.Presentations.Add
    Set newSectionProperties = newPresentation.SectionProperties

    For Each oldSlide In oldPresentation.Slides
        oldSlide.Copy
        ' Would lead to wrong sectioning: Set newSlide = newPresentation.Slides.Paste.Item(1)
        Set newSlide = newPresentation.Slides.Paste(newPresentation.Slides.Count + 1).Item(1)

        For i = 1 To oldSectionProperties.Count
            If oldSectionProperties.FirstSlide(i) = oldSlide.SlideIndex Then
                newSectionProperties.AddBeforeSlide _
                    newSlide.SlideIndex, _
                    oldSectionProperties.Name(i)
                Exit For
            End If
        Next i
    Next oldSlide
End Sub