如何从一组PPTS中复制特定幻灯片?

时间:2016-05-03 06:31:16

标签: c# interop powerpoint

我有一个应用程序可以识别某些标准的幻灯片,并希望将这些幻灯片复制到单个PPTX文件中。我有用于复制幻灯片的openxml代码,它工作正常,但在输出文件大小增加时花费了太多时间。所以我决定转向互操作。以下代码用于处理幻灯片。

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();
        }
    }
}

我已经尝试了

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

在两种情况下都复制了内容,但幻灯片的背景和格式在输出中消失了。在应对时我是否有任何遗漏。

1 个答案:

答案 0 :(得分:3)

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

        }

你可以试试这个。