在ppt中保存幻灯片时保留源模板

时间:2015-02-25 13:04:33

标签: c# vsto office-interop

我正在尝试保存选定的幻灯片,因此它不会保留我的源模板。如何在保存幻灯片时保留现有模板

   private void SaveSelectedSlide_Click(object sender, RibbonControlEventArgs e)
    {
        try
        {
            PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
            PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var temporaryPresentation = Globals.ThisAddIn.Application.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
            Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = ppApp.ActivePresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

            for (int i = 1; i <= ppslr.Count; i++)
            {
                var sourceSlide = ppslr[i];
                sourceSlide.Copy();

                var design = sourceSlide.Design;
                temporaryPresentation.Slides.Paste();


            }

            temporaryPresentation.SaveAs("Temporary", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoTrue);

            temporaryPresentation.Close();

        }
        catch (COMException Ex)
        {
            Debug.WriteLine("Some problem" + Ex.Message + Ex.StackTrace);
            MessageBox.Show("PLease enter text ");
        }

    }

1 个答案:

答案 0 :(得分:3)

我想我得到了你想要的东西。粘贴新幻灯片时,保存新的 SlideRange 。然后分配 源幻灯片设计

PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var temporaryPresentation = Globals.ThisAddIn.Application.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = ppApp.ActivePresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

for (int i = 1; i <= ppslr.Count; i++)
{
    var sourceSlide = ppslr[i];
    sourceSlide.Copy();

    var design = sourceSlide.Design;                    
    SlideRange sr = temporaryPresentation.Slides.Paste(); // get newly created slideRange
    sr.Design = sourceSlide.Design; // manually set design
}
temporaryPresentation.SaveAs("Temporary", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoTrue);
temporaryPresentation.Close();

它对我有用。如果这是预期的行为,请告诉我!