How should I copy and paste a powerpoint slide KEEPING SOURCE FORMATTING with VBScript?

7.1k Views Asked by At

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??

3

There are 3 best solutions below

0
On

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.

0
On

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
0
On

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:

  1. I start out with an empty shell presentation that I then populate
  2. I have a "blank slide" template that I'm using for most of the slides.
  3. I also sometimes have other one-off slides that I need to include.

With regard to activating the proper presentation I used the answer from here:

http://answers.microsoft.com/en-us/msoffice/forum/msoffice_powerpoint-msoffice_custom/powerpoint-vba-making-right-window-active/32c9fa95-fa1d-46f8-912a-09771a63a27a Code