Reputation:
I'm trying to write a subroutine that will copy and paste all (and later only some) of the slides from a powerpoint file to another keeping source formatting.
This is the closest I have gotten:
Dim objPowerpointApp, objPresentations
Set objPowerpointApp = CreateObject("Powerpoint.Application")
Set objPresentations = objPowerpointApp.Presentations
Dim objNewPresentation
Set objNewPresentation = objPresentations.Open(strTemplateFile, 0, 0, -1)
Dim objOldPresentation : Set objOldPresentation = objPresentations.Open(tempSlideSet.Path, 0, 0, 0)
Dim tempCurrentSlide
For Each tempCurrentSlide in objOldPresentation.Slides
tempCurrentSlide.Copy
objNewPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
Next
It throws no errors and even pastes the correct slide masters, but it doesn't actually paste any of the slides.
I've also tried this:
Dim objPowerpointApp, objPresentations
Set objPowerpointApp = CreateObject("Powerpoint.Application")
Set objPresentations = objPowerpointApp.Presentations
Dim objNewPresentation
Set objNewPresentation = objPresentations.Open(strTemplateFile, 0, 0, -1)
Dim objOldPresentation : Set objOldPresentation = objPresentations.Open(tempSlideSet.Path, 0, 0, 0)
Dim tempCurrentSlide
For Each tempCurrentSlide in objOldPresentation.Slides
tempCurrentSlide.Copy
objNewPresentation.Slides.Paste
objNewPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
Next
Which pastes the correct slide master as well as the slide, but the slide is pasted with the destination formatting.
Naturally, this pastes all the slides correctly, but without any of the source formatting:
Dim objPowerpointApp, objPresentations
Set objPowerpointApp = CreateObject("Powerpoint.Application")
Set objPresentations = objPowerpointApp.Presentations
Dim objNewPresentation
Set objNewPresentation = objPresentations.Open(strTemplateFile, 0, 0, -1)
Dim objOldPresentation : Set objOldPresentation = objPresentations.Open(tempSlideSet.Path, 0, 0, 0)
Dim tempCurrentSlide, lngSlideIndex
For Each tempCurrentSlide in objOldPresentation.Slides
lngSlideIndex = objNewPresentation.Slides.Count
objNewPresentation.Slides.InsertFromFile tempSlideSet.Path, lngSlideIndex
Next
From what I can find in documentation and others' experience, the first option should work as I need, so I really am stuck at this point.
How can I copy and paste a powerpoint slide using VBScript and keep its source formatting??
Upvotes: 2
Views: 7479
Reputation: 1
You first need to Activate the Window of the target slidedeck before pasting with the "PasteSourceFormatting" option
when TargetPPT is the destination presentation in VBA, and if I want to copy all slides of SourcePPT:
SourcePPT.Slides.Range.Copy
TargetPPT.Windows(1).Activate
TargetPPT.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
The slides are pasted at the current active slide
Use
Application.ActiveWindow.View.GotoSlide (5)
to position f.i. on slide 5 first, and paste the new slides after slide 5.
Upvotes: 0
Reputation: 1
This helped me resolve a long-term frustration with PowerPoint. Thank you so much for posting this. A nuance I found is was important to activate the presentation I wanted to affect before pasting, otherwise the slide could end up in the wrong place. In my case I'm juggling things between three different presentations:
With regard to activating the proper presentation I used the answer from here:
Upvotes: 0
Reputation:
To anyone struggling with the same issue, here is the subroutine that I managed to create. The objDestPresentation
should be a Presentation Object and the sourceSlideRange
should be a SlideRange Object.
This will copy the slides from sourceSlideRange
to the end of the objDestPresentation
. Hopefully this will save someone some of the struggle that I went through!
Private Sub InsertSlide_Source(ByRef objDestPresentation, byVal sourceSlideRange)
sourceSlideRange.Copy
If Not objDestPresentation.Slides.Count = 0 Then
objDestPresentation.Windows(1).View.GotoSlide(objDestPresentation.Slides.Count)
End If
objDestPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
End Sub
Upvotes: 1