其他分享
首页 > 其他分享> > 将幻灯片保存在ppt中时保留源模板

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

作者:互联网

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

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

    }

解决方法:

我想我得到了你想要的.粘贴新幻灯片时,保存新的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();

它为我工作.请让我知道这是否是预期的行为!

标签:c,vsto,office-interop
来源: https://codeday.me/bug/20191120/2045829.html