madan
madan

Reputation: 783

How to copy specific slides from a group of PPTS?

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.

Upvotes: 3

Views: 1740

Answers (1)

madan mohan
madan mohan

Reputation: 78

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");

}

You can try this one.

Upvotes: 3

Related Questions