问题
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