在多个打开的演示文稿中替换一张幻灯片

时间:2019-08-14 20:59:28

标签: powerpoint-vba

我整理了一些VBA代码,希望在几个公开演示中替换一张相同的幻灯片。

此操作将新幻灯片粘贴到末尾,而不是粘贴到旧幻灯片的删除位置。此外,我需要在所有打开的演示文稿中进行此操作。请注意,我通过SlideID识别幻灯片。

Sub ReplaceOneSlide()
ActivePresentation.Slides.FindBySlideID(1846).Delete
Dim sourcePresentation As Presentation
    On Error Resume Next
    Set sourcePresentation = Application.Presentations("X:\Marketing Presentations (Final) \Slide Library\Slide Library.pptm") 'change the name accordingly
    If sourcePresentation Is Nothing Then
        MsgBox "Source presentation not found!", vbExclamation
        Exit Sub
    End If
    On Error GoTo 0

    Dim vSlideIDs As Variant
    vSlideIDs = Array(1846) 'change the slide IDs accordingly

    Dim i As Long
    For i = LBound(vSlideIDs) To UBound(vSlideIDs)
        sourcePresentation.Slides.FindBySlideID(vSlideIDs(i)).Copy
        ActivePresentation.Slides.Paste
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

为什么要使用数组保存一个值?

  Dim vSlideIDs As Variant
    vSlideIDs = Array(1846) 'change the slide IDs accordingly

    Dim i As Long
    For i = LBound(vSlideIDs) To UBound(vSlideIDs)
        sourcePresentation.Slides.FindBySlideID(vSlideIDs(i)).Copy
        ActivePresentation.Slides.Paste
    Next i

而不是尝试这样的代码(主要是空中代码):

 Dim lSlideIDs As Long
 Dim oSld as Slide
 Dim lIndex as long

    lSlideIDs = 1846 'change the slide IDs accordingly
        lIndex = sourcePresentation.Slides.FindBySlideID(lSlideIDs).SlideIndex
        sourcePresentation.Slides.FindBySlideID(lSlideIDs).Copy
        ActivePresentation.Slides.Paste
        Set oSld = ActivePresentation.Slides(ActivePresentation.Slides.Count)
        oSld.MoveTo lIndex
    'Next i ' commenting this out, per OP's comment; my bad