如何将一个PowerPoint演示文稿的特定幻灯片复制到另一个?

时间:2018-09-25 04:38:31

标签: c# .net com powerpoint

我有下面的代码,使用该代码尝试将一个演示文稿的特定幻灯片复制到另一个演示文稿,但是没有发生任何错误。

source = ppt.Presentations.Open(filename, MsoTriState.msoTrue, 
MsoTriState.msoTrue, MsoTriState.msoTrue);

target = ppt.Presentations.Open(targetname, MsoTriState.msoTrue, 
MsoTriState.msoTrue, MsoTriState.msoTrue);

sourceSlideRange = source.Slides.Count;

for (int i = 3; i < sourceSlideRange; i++)
{
source.Slides[i].Copy();
target.Windows[1].Activate();
target.Slides[1].Select();

target.Application.CommandBars.ExecuteMso("PasteSourceFormatting");
}

我的代码有什么问题?请提出建议。谢谢

1 个答案:

答案 0 :(得分:0)

由于我的要求是仅在演示文稿中保留特定的幻灯片,并且张贴的复制代码无法正常工作,因此我尝试从演示文稿中删除不需要的幻灯片,仅保留我需要的幻灯片。下面是解决目的的代码

Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
ppt.Visible = MsoTriState.msoTrue;
ppt.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMinimized;

source = ppt.Presentations.Open(filename);

target = ppt.Presentations.Open(targetname, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);

sourceSlideRange = source.Slides.Count;

for (int i = 3; i < sourceSlideRange; i++)
    {
      source.Slides[i].Delete();
      source.Save();

    }

source.Close();