none
How to copy specific slides from a group of PPTS? RRS feed

  • Question

  • I have an application which identify some slides with some criteria and want to copy those slides to a single PPTX file. I have openxml code for copy slides and it working perfectly but taking too much time when output file size increases. So i decided to move to interop for coping. Following code is for coping slide.

    using Microsoft.Office.Core;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    
    
    namespace CloneSlide
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    PowerPoint.Application app = new PowerPoint.Application();
                    PowerPoint.Presentation currPresentation = null;
                    PowerPoint.Presentation currPresentationop = null;
    
                    string inputFileName = @"C:\Users\user\Desktop\Blannkdocs\ppt\Input.pptx";
                    //PowerPoint.Presentations presentations = app.Presentations;
                    //var readOnly = Microsoft.Office.Core.MsoTriState.msoTrue;
                    //var untitled = Microsoft.Office.Core.MsoTriState.msoTrue;
                    //var withwindow = Microsoft.Office.Core.MsoTriState.msoFalse;
                    //string chkfileforpassword = inputFileName + "::" + "\"\"" + "::" + "\"\"";
                    //currPresentation = presentations.Open(chkfileforpassword, readOnly, untitled, withwindow);
                    //currPresentation.Slides[1].Copy();
    
                    string outputFileName = @"C:\Users\user\Desktop\Blannkdocs\ppt\Presentation1.pptx";
                    PowerPoint.Presentations presentationsop = app.Presentations;
                    currPresentationop = presentationsop.Open(outputFileName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
                    //currPresentationop.Slides.Paste(1);
                    currPresentationop.Slides.InsertFromFile(inputFileName, 1, 1, 1);
                    System.Threading.Thread.Sleep(4000);
                    currPresentationop.Save();
                    app.Quit();
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp);
                }
                Console.WriteLine("Execution Complete");
                Console.ReadLine();
            }
        }
    }

    I have already tried with

            //currPresentationop.Slides.Paste(1);
            currPresentationop.Slides.InsertFromFile(inputFileName, 1, 1, 1);

    in both the case content are copied but the background and formatting of the slides were disappeared in the output. Is there anything I missed to add while coping.

    Tuesday, May 3, 2016 7:14 AM

Answers

  • Hi Madan,

    The above sample is writing with VBA and copy the slide from presentation1. Here are steps for you running the sample above:

    1. Create presentation1

    2. Set the background on the first slide, save the presentation1

    3. Create a new presentation and copy the code above into the VBE of new presentaion

    4. Run the code

    Did you get any error message? And here is an console demo writing with C# for your reference:

    using PPT = Microsoft.Office.Interop.PowerPoint;
    
            public void Main()
            {
                PPT.Application app = new PPT.Application();
                app.Visible = MsoTriState.msoCTrue;
                PPT.Presentation ppt1 = app.Presentations.Open(@"C:\Presentation1.pptx");
                ppt1.Slides[1].Copy();
    
                PPT.Presentation ppt2 = app.Presentations.Open(@"C:\Presentation2.pptx");
                ppt2.Windows[1].View.GotoSlide(1);
    
                app.CommandBars.ExecuteMso("PasteSourceFormatting");
    
            }
    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, May 6, 2016 7:26 AM
    Moderator

All replies

  • Interop is a good choice, also, I found another alternative way for copying slides with background and formatting with a free powerpoint API, you can check and give it a try: How to duplicate slides in presentation

    Hope this helps.

    Tuesday, May 3, 2016 9:23 AM
  • Hi thanks for your reply.
    it works but it support only for 10 slide ppt.
    Tuesday, May 3, 2016 11:20 AM
  • Hi Madan,

    As far as i know, there is no such object model we can copy/paste the slide from one presentation to another keep source formatting. 

    However, as a workaround, we can use the command provided by the PowerPoint. For example, here is an example that copy the slide from presentation1 to the active presentation and paste it into second slide:

    Application.Presentations("presentation1").Slides(1).Copy
    Application.ActivePresentation.Windows(1).View.GotoSlide 1
    Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
    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, May 4, 2016 7:28 AM
    Moderator
  • Hi @Fei Xue thanks for your reply.

    I tried the above line but fail to include the library for Application can you please help me with a small program.

    Wednesday, May 4, 2016 8:53 AM
  • Hi Madan,

    The above sample is writing with VBA and copy the slide from presentation1. Here are steps for you running the sample above:

    1. Create presentation1

    2. Set the background on the first slide, save the presentation1

    3. Create a new presentation and copy the code above into the VBE of new presentaion

    4. Run the code

    Did you get any error message? And here is an console demo writing with C# for your reference:

    using PPT = Microsoft.Office.Interop.PowerPoint;
    
            public void Main()
            {
                PPT.Application app = new PPT.Application();
                app.Visible = MsoTriState.msoCTrue;
                PPT.Presentation ppt1 = app.Presentations.Open(@"C:\Presentation1.pptx");
                ppt1.Slides[1].Copy();
    
                PPT.Presentation ppt2 = app.Presentations.Open(@"C:\Presentation2.pptx");
                ppt2.Windows[1].View.GotoSlide(1);
    
                app.CommandBars.ExecuteMso("PasteSourceFormatting");
    
            }
    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, May 6, 2016 7:26 AM
    Moderator
  • Hi Fei Xue,

    As my application is a console application I tried with console code. It working but very inconsistent.  I added the code to my application. I have to copy around 300 slides from different PowerPoint decks to a single PowerPoint Deck. I run the tool several times but every time it stuck in different position. For first run it stopped on file 55, on second 11, on third 97.  While coping it unable to copy a slide then after it continuously got error for next slide. I think I am missing something please help me to resolve the issue

    This is the error what I got

    View (unknown member): Integer out of range. 300 is not in the valid range of 1 to 299.

    This is because it fails to copy the 299th slide to destination so it giving the error on 300th slide.


    Monday, May 9, 2016 2:01 PM
  • Hi Madan,

    Did you fix this issue now? If not, 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.

    Thursday, May 19, 2016 10:09 AM
    Moderator
  • Hi Fei,

    Till now I not faced any issue by using below code. It is almost same as your code I just activate the destination power point before writing to it. It is a part of my application.

    private bool Copy(PPT.Presentation pptPresentation, int slideId, ref PPT.Presentation outputDeck)
    {
    	int slideCountBeforeWrite = outputDeck.Slides.Count;
    
    	pptPresentation.Slides[slideId].Copy();
    	outputDeck.Windows[1].Activate(); //Ensures that the destination ppt is activaed for writting 
    	outputDeck.Windows[1].View.GotoSlide(outputDeck.Slides.Count); // copy the slide to the last position of the powerpoint 
    	pptApplication.CommandBars.ExecuteMso("PasteSourceFormatting");
    	
    	if (slideCountBeforeWrite == outputDeck.Slides.Count-1) //check whether slide count increase or not
    	{
    		return true;
    	}
    	return false;
    }





    Tuesday, May 24, 2016 6:31 AM
  • Here is a solution for your reference - Merge Selected Slides to a Single Presentation in C#, VB.NET

    Presentation ppt = new Presentation();
    Presentation ppt1 = new Presentation("sample_01.pptx",FileFormat.Pptx2010);
    Presentation ppt2 = new Presentation("sample_02.pptx", FileFormat.Pptx2010);
    ppt.Slides.RemoveAt(0);
    //append all slides in ppt1 to ppt
    for (int i = 0; i < ppt1.Slides.Count; i++)
    {
        ppt.Slides.Append(ppt1.Slides[i]);
    }
    //append the specified slide in ppt2 to ppt
    ppt.Slides.Append(ppt2.Slides[1]);
    //save and launch the file
    ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
    
    Wednesday, May 25, 2016 6:53 AM
  • Hi Fei Xue
    I was stuck with another problem. Also put the question in the same forum, I got an answer also but it was not complete solution.Question link(https://social.msdn.microsoft.com/Forums/en-US/89187ba0-a417-49d8-85b1-406c52bf0046/how-to-add-multiple-bullet-to-a-shape-in-power-point?forum=officegeneral)  for maintain source formatting there is a argument "PasteSourceFormatting". I want to know is there any such command for extracting text from a SmartArt with proper indentation of text.

      
    Friday, May 27, 2016 5:39 AM
  • Hi Scott4DotNet

    For this code we have to purchase 3rd party DLL (Spire) if i use free DLL then it giving Error if the any ppt file have more than 10 slide It is not allowing to go further.

     

    Friday, May 27, 2016 6:07 AM