Replacing one slide in several open presentations

拜拜、爱过 提交于 2020-03-05 06:07:29

问题


I cobbled together some VBA code in the hopes of replacing a single, identical slide in several open presentations.

This pasted the new slide at the end rather than where the old slide was deleted. Also, I'd need this to happen with all open presentations. Note that I identify slides by 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:


Why are you using an array to hold one value?

  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

Instead try something like this (largely air)code:

 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


来源:https://stackoverflow.com/questions/57501968/replacing-one-slide-in-several-open-presentations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!