Joel
Joel

Reputation: 1678

Pasting multiple slide ranges with ExecuteMso("PasteSourceFormatting") results in only the last one beeing pasted

Im am creating a PowerPoint Add-In in Visual Basic with Visual Studio as VSTO Add-In. With one of the buttons in my Add-In I want to add slides from two different presentations to my current one while keeping the original source formatting. I use CommandBars.ExecuteMso("PasteSourceFormatting") for my function but apparently PowerPoint pastes the slides at the very end of the Sub procedure and not when the function is called.

My code looks like this:

Private Sub ButtonInsertSong_Click(sender As Object, e As RibbonControlEventArgs) Handles ButtonInsertSong.Click

        Dim srcPath1 As String = "" 'would be an absolute path
        Dim srcPath2 As String = "" 'would be an other absolute path

        Dim pptApp As PowerPoint.Application
        Dim destPPT As Presentation
        Dim srcPPT1 As Presentation
        Dim srcPPT2 As Presentation

        pptApp = GetObject([Class]:="PowerPoint.Application")

        destPPT = pptApp.ActivePresentation
        srcPPT1 = pptApp.Presentations.Open(srcPath1, MsoTriState.msoTrue,, MsoTriState.msoFalse)
        srcPPT2 = pptApp.Presentations.Open(srcPath2, MsoTriState.msoTrue,, MsoTriState.msoFalse)

        srcPPT1.Slides.Range().Copy()
        pptApp.CommandBars.ExecuteMso("PasteSourceFormatting")
        'pptApp.CommandBars.ReleaseFocus()
        'destPPT.Save()

        srcPPT2.Slides.Range().Copy()
        pptApp.CommandBars.ExecuteMso("PasteSourceFormatting")
        'pptApp.CommandBars.ReleaseFocus()
        'destPPT.Save()

        srcPPT1.Close()
        srcPPT2.Close()

    End Sub

I found two similar problems where they suggested either using CommandBars.ReleaseFocus() which didnt change anything for me. The other solution would be to save the current presentation each time after pasting some slides to it which would work in theory but I want this Add-In to work with a newly created presentation which has not been saved before and therefore doesnt have a name or path yet.

I thought of a third option which I havent tried out yet but I guess it would work. I would create a new temporary presentation where I can paste both slide ranges seperately and save the temporary presentation twice. After that I would copy the slides from the temporary presentation to the current one and I guess this should work fine but it would be pretty slow to do this.

So now I am wondering if there is a way to force the ExecuteMso("PasteSourceFormatting") to execute right after beeing called wihtout saving my presentation so I dont have to do some weird extra work which slows down the whole process.

Upvotes: 0

Views: 134

Answers (0)

Related Questions