none
Copy slide with background picture from another presentation RRS feed

  • Question

  • Hi

    In one of my projects we have created a function where the user can copy selected slides from another presentation into the active presentation.

    This works very well for all slides except when they hold a background image.
    The background image doesn't follow to the target presentation.

    This also happens if you manually open both presentations and use copy/paste. The background image is lost.

    In our code we use the function activePresentation.Slides.InsertFromFile(<filename of other presentation>, index, fromSlide,  toSlide)

    Is there a way to also copy the background image if one exist on the selected slide?


    Best Regards Peter Karlström Midrange AB, Sweden


    Thursday, November 26, 2015 4:12 PM

Answers

  • Hi Peter,

    I am still failed to reproduce this issue. I refer to the new slide successfully after execute the past command. Here is the test code for your reference:

    Private Sub btnCopySlide_Click(sender As Object, e As EventArgs) Handles btnCopySlide.Click Dim pwtApp As New PPT.Application Dim sourcePres As PPT.Presentation = pwtApp.Presentations.Open("C:Presentation1.pptx") Dim targePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation2.pptx") pwtApp.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue sourcePres.Slides(1).Copy() targePres.Windows(1).Activate() targePres.Slides(1).Select() pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")

    pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting") targePres.Slides(3).ApplyTheme("C:\Presentation1.thmx")

    I suggest that you write a loop to check the slide count to see whether it is helpful.

      Private Sub btnCopySlide_Click(sender As Object, e As EventArgs) Handles btnCopySlide.Click
            Dim pwtApp As New PPT.Application
            Dim sourcePres As PPT.Presentation = pwtApp.Presentations.Open("C:Presentation1.pptx")
            Dim targePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation2.pptx")
    
            pwtApp.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue
            sourcePres.Slides(1).Copy()
            targePres.Windows(1).Activate()
            targePres.Slides(1).Select()
            pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
    
    	pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
    
           'wait for completing the past action 
            While targePres.Slides.Count < slideCount + 1
                Debug.Print("wait...")
                Threading.Thread.Sleep(100)
            End While
            targePres.Slides(3).ApplyTheme("C:\Presentation1.thmx")
    
    
    

    If you still have the issue, would you mind sharing a runnable code sample to help us to reproduce this issue?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 2, 2015 3:21 AM
    Moderator

All replies

  • Hi Peter,

    >>Is there a way to also copy the background image if one exist on the selected slide?<<

    Yes. When we paste the slide, there are three options like figure below:

    Since there is no object model corresponding this feature, as a workaround we can execute the command. Here is the code for your reference:

    Application.CommandBars.ExecuteMso "PasteSourceFormatting"

    Please ensure that the presentation copy to is active before you execute this command.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 27, 2015 6:57 AM
    Moderator
  • Hi

    OK, Thanks.

    Pity about the object model, but I'll try the UI-commands instead and see if this would be enough to satisfy the customer.


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, November 27, 2015 8:17 AM
  • Hi again

    FYI, the link for the survey doesn't work any longer.


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, November 27, 2015 8:18 AM
  • Hi Fei

    I've tried your solution but I must say it feels a little "unstable" due to som strange errors on the response to the use of <presentation>.Slides-collection after the paste-operation.

    This is a sample of my code:

    Try
        sourcePres.Slides(nSlide).Copy()
        Windows.Forms.Application.DoEvents()
        targetPres.Slides(idx).Select()
        targetPres.Windows(1).Activate()
        pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
        idx += 1
    Catch ex As Exception
        MsgBox("Ett fel uppstod då sidan skulle kopieras. Fel: " & ex.Message, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, My.Application.Info.ProductName & " Ver: " & My.Application.Info.Version.ToString)
    End Try

    If the target presentation (targetPres) contains 1 slide before the use of the function, it still contains only 1 slide after the execution of pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting").

    In some scenarios we let the user deside if they want to apply the source presentations theme on the imported slide. Since the new slide don't seem to appear in the Slides-collection of the target presentation, we get an error when we try to apply it.

    Why is the slides-collection not updated?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden


    Friday, November 27, 2015 11:00 AM
  • Hi Peter,

    Thanks for the detail information for this issue and what's the error message you got?

    I made a simple demo to reproduce this issue in PowerPoint 2013, however failed. Here are my steps:

    1. Create two presentations and both of them contain 1 slide. And on of them have the background

    2. Create a windows form and put a button to execute the code below:

        Private Sub btnCopySlide_Click(sender As Object, e As EventArgs) Handles btnCopySlide.Click
            Dim pwtApp As New PPT.Application
            Dim sourcePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation1.pptx")
            Dim targePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation2.pptx")
    
            pwtApp.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue
            sourcePres.Slides(1).Copy()
            targePres.Windows(1).Activate()
            targePres.Slides(1).Select()
            pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
    
        End Sub
     

    Did the sample below works for you?

    In addition, I have reported about the link for the survey and haven't got response. If there is any update, I would be back.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 1, 2015 3:19 AM
    Moderator
  • Hello Fei

    As you can see in my sample, I have a counter called idx which is added by 1 for every succesful slide insertion into the presentation.

    Soo, when I for example have 2 slides when I start, idx = 2.

    After successfully adding the selected slide with the command pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting"), idx is updated to 3.

    If I after this want to access the newly added slide I would use something like targetPres.Slides(idx) in order to do this, like this:

    Try
       targetPres.Slides.Range(idx).ApplyTheme(sourceFile)
    Catch ex As Exception
       MsgBox("Error: " & ex.Message)
    End Try

    This is here the error occurs, by stating that the presentation only contains 2 slides, eventhough we just successfully have added one.
    Looking at the presentation the new slide is actually added to the presentation.

    So, my question is: Why is the target presentations Slide collection corrupted?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden

    Tuesday, December 1, 2015 7:39 AM
  • Hi Alice

    Thanks for your reply

    This was interesting, but since this involves a 3:rd party component, I'll have to wait and see if this can be solved using projects existing components first.

    Thanks anyway


    Best Regards Peter Karlström Midrange AB, Sweden

    Tuesday, December 1, 2015 9:02 AM
  • Hi Peter,

    I am still failed to reproduce this issue. I refer to the new slide successfully after execute the past command. Here is the test code for your reference:

    Private Sub btnCopySlide_Click(sender As Object, e As EventArgs) Handles btnCopySlide.Click Dim pwtApp As New PPT.Application Dim sourcePres As PPT.Presentation = pwtApp.Presentations.Open("C:Presentation1.pptx") Dim targePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation2.pptx") pwtApp.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue sourcePres.Slides(1).Copy() targePres.Windows(1).Activate() targePres.Slides(1).Select() pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")

    pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting") targePres.Slides(3).ApplyTheme("C:\Presentation1.thmx")

    I suggest that you write a loop to check the slide count to see whether it is helpful.

      Private Sub btnCopySlide_Click(sender As Object, e As EventArgs) Handles btnCopySlide.Click
            Dim pwtApp As New PPT.Application
            Dim sourcePres As PPT.Presentation = pwtApp.Presentations.Open("C:Presentation1.pptx")
            Dim targePres As PPT.Presentation = pwtApp.Presentations.Open("C:\Presentation2.pptx")
    
            pwtApp.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue
            sourcePres.Slides(1).Copy()
            targePres.Windows(1).Activate()
            targePres.Slides(1).Select()
            pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
    
    	pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
    
           'wait for completing the past action 
            While targePres.Slides.Count < slideCount + 1
                Debug.Print("wait...")
                Threading.Thread.Sleep(100)
            End While
            targePres.Slides(3).ApplyTheme("C:\Presentation1.thmx")
    
    
    

    If you still have the issue, would you mind sharing a runnable code sample to help us to reproduce this issue?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 2, 2015 3:21 AM
    Moderator
  • Hello Fei

    I see now that we are using different versions of Office, and probably also different versions in Visual Studio.

    The customer and this project is using Office 2010, and Visual Studio 2010.

    I will test your suggestion later today, anyway.


    Best Regards Peter Karlström Midrange AB, Sweden

    Wednesday, December 2, 2015 6:31 AM
  • Hello Fei

    Here is a more complete scenario and code from the project.

    The code is from a Windows form shown on the attached image.
    The code is from the event of the OK-button click.

    Some facts about the form thats is referred to in the code:
    lstFDO - Listbox with files containing slides to choose from (Välj Fördefinierat objekt)
    lstSlidelist - Checked listbox containing the slides in the selected presentation (Välj sida/sidor)
    chkTheme - Checkbox for theme handling (Använd måltema - Use target theme)

    Also, targetPres is the currently opent presentation i Powerpoint and sourcePres is the selected and opened source presentation.

    The code is partly translated to english for better understanding.

    Also, finally, as I mentioned earlier;
    This is a Powerpoint 2010 Add in created in Visual Studio 2010.

    Dialog

    And here is the code:

        Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
    
            Dim fdoFile As String 'source presentation fullname
    
            If lstSlidelist.CheckedItems.Count = 0 Then
                MsgBox("You must select a slide.", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, My.Application.Info.ProductName & ", Ver: " & My.Application.Info.Version.ToString)
                Exit Sub
            Else
                Dim tSlide As String
                Dim nSlide As Int16 = 1
                Dim idx As Int16 = targetPres.Slides.Count
                fdoFile = presPath & lstFDO.Text
                Dim sourcePres As PowerPoint.Presentation = pwtApp.Presentations.Open(fdoFile, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse)
                For Each itm In lstSlidelist.CheckedItems
                    tSlide = itm.ToString
                    tSlide = tSlide.Remove(0, 5)
                    If IsNumeric(tSlide) Then
                        nSlide = CInt(tSlide)
                        If chkTheme.Checked Then
                            Try
                                sourcePres.Slides(nSlide).Copy()
                                Windows.Forms.Application.DoEvents()
                                targetPres.Slides.Paste()
                                idx += 1
                            Catch ex As Exception
                                MsgBox("An error occured when inserting the selected slide. Error: " & ex.Message, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, My.Application.Info.ProductName & " Ver: " & My.Application.Info.Version.ToString)
                            End Try
                        Else
                            Try
                                sourcePres.Slides(nSlide).Copy()
                                Windows.Forms.Application.DoEvents()
                                targetPres.Slides(idx).Select()
                                targetPres.Windows(1).Activate()
                                pwtApp.CommandBars.ExecuteMso("PasteSourceFormatting")
                                idx += 1
                            Catch ex As Exception
                                MsgBox("An error occured when inserting the selected slide. Error: " & ex.Message, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, My.Application.Info.ProductName & " Ver: " & My.Application.Info.Version.ToString)
                            End Try
                        End If
                        If Not chkTheme.Checked Then
                            Try
                                targetPres.Slides.Range(idx).ApplyTheme(fdoFile)
                            Catch ex As Exception
                                MsgBox("An error occured when trying to apply theme from source presentation. Error: " & ex.Message, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, My.Application.Info.ProductName & " Ver: " & My.Application.Info.Version.ToString)
                            End Try
                        End If
                    End If
                Next
                Try
                    sourcePres.Close()
                Catch ex As Exception
    
                End Try
    
                GC.Collect()
    
                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Me.Close()
            End If
    
        End Sub
    

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden

    Thursday, December 3, 2015 8:14 AM
  • Hi Peter,

    Thanks for the detail information for this issue.

    I am trying to reproduce this issue using Office 2010 and Visual Studio 2010, however still failed. What's the exact error message did you get?

    In addition, would you mind sharing a runnable demo to help us narrow down this issue? You can upload it via OneDrive and please remove the sensitive information before you uploading.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 4, 2015 7:02 AM
    Moderator
  • Hi Fei

    Strange.

    I will test a packaged application on another client to ensure that it's not a client issue.
    Unfortunately I will not be able to test this until end next week.

    I'll get back to you after these tests.


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, December 4, 2015 2:52 PM
  • Hi Peter,

    Have you fix the issue now? If the issue still exits, please feel free to let us know the detail which block you.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 7, 2015 8:29 AM
    Moderator