C#VSTO-PowerPoint使用源格式复制/粘贴幻灯片

时间:2012-03-22 16:37:28

标签: c# vsto powerpoint

快速提问,希望有人可以在这里提供帮助。我正在尝试将幻灯片从一个powerpoint演示文稿复制并粘贴到下一个。我现在已经把它复制并粘贴到正确的幻灯片上,但我的问题是它只是粘贴在演示文稿的最后一张幻灯片上。我已经尝试了/ foreach循环,但只是给我一张幻灯片,我想知道它是不是CommandBars。但是我看到它们被用来重置for / foreach循环中的幻灯片。有什么想法吗?

    public void AppendPPTX(string newContent)
    {
        int sourceSlideRange = 0;
        int targetSlideRange = Application.ActiveWindow.Presentation.Slides.Count;
        PowerPoint.Presentation target;
        PowerPoint.Presentation source;

        try
        {
            target = Application.ActivePresentation;
            source = Application.Presentations.Open(newContent, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);

            sourceSlideRange = source.Slides.Count + 1; //otherwise I was just getting the second to the last slide

            for (int i = 1; i < sourceSlideRange; i++)
            {
                source.Slides[i].Copy();
                target.Slides[targetSlideRange].Select();
                target.Application.CommandBars.ExecuteMso("PasteSourceFormatting");
            }
            source.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Error opening PowerPoint, corruption found inside the powerpoint file. " +
                            Environment.NewLine + "The corrupted file has been deleted." + Environment.NewLine +
                            "Please attempt to redownload file.",
                            "Error Opening PowerPoint",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

2 个答案:

答案 0 :(得分:2)

尝试在PasteSourceFormatting之后保存PPT。我确实遇到了同样的问题。

答案 1 :(得分:1)

我在Application.DoEvents()之后使用了PasteSourceFormatting,它运行正常!