如何使用与C#的互操作将*.ppt、*.pptx文件保存为*.wmv?

如何使用与C#的互操作将*.ppt、*.pptx文件保存为*.wmv?,c#,.net,interop,powerpoint,wmv,C#,.net,Interop,Powerpoint,Wmv,我尝试使用下一个代码执行此操作: using Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using System.IO; using Microsoft.Office.Interop.PowerPoint; namespace SavePPT { class Program { static void Main(string

我尝试使用下一个代码执行此操作:

using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;

namespace SavePPT
{
        class Program
        {
            static void Main(string[] args)
            {
                Application app = new PowerPoint.Application();
                var pres = app.Presentations;
                var file = pres.Open(@"C:\Presentation1.pptx", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
                file.SaveCopyAs(@"C:\presentation1.wmv", PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoCTrue);

                app.Quit();

            }
        }
}

但是这个解决方案创建了大小为0 KB的文件,当然我不能播放它

PowerPoint的SaveCopyAs方法似乎不支持ppSaveAsWMV。它明确说明了它在for SaveCopyAs上支持哪些,其中不包括ppSaveAsWMV常量。

我找到了解决方案:

using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;

namespace SavePPT
{
        class Program
        {
            static void Main(string[] args)
            {
                string fileName = @"C:\Presentation1.pptx";
                string exportName = "video_of_presentation";
                string exportPath = @"C:\{0}.wmv";

                Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
                ppApp.Visible = MsoTriState.msoTrue;
                ppApp.WindowState = PpWindowState.ppWindowMinimized;
                Microsoft.Office.Interop.PowerPoint.Presentations oPresSet = ppApp.Presentations;
                Microsoft.Office.Interop.PowerPoint._Presentation oPres = oPresSet.Open(fileName,
                            MsoTriState.msoFalse, MsoTriState.msoFalse,
                            MsoTriState.msoFalse);
                try
                {
                    oPres.CreateVideo(exportName);
                    oPres.SaveCopyAs(String.Format(exportPath, exportName),
                        PowerPoint.PpSaveAsFileType.ppSaveAsWMV,
                        MsoTriState.msoCTrue);
                }
                finally
                {
                    ppApp.Quit();
                }
            }
        }
}
这段代码保存文件,但有一些延迟。谢谢你的帮助。

试试看

while (oPres.CreateVideoStatus == PpMediaTaskStatus.ppMediaTaskStatusInProgress)
{
    Thread.Sleep(100);
}

什么样的应用程序{命令行、win表单、web表单}?您当前是否正在运行VStudio(哪个版本)?如果您的操作系统是Win7,您是否正在执行“以管理员身份运行”?Office的哪个版本?例如,2003年实际上并不支持ppSaveAsWMV。它只是控制台应用程序,VS 2010 Ultimate,Win7以管理员身份运行。PowerPoint 2010想补充一点,我的演示文稿不是空的,我有超过25张幻灯片,大小约241KB。谢谢。你能建议另一种将*.ppt转换为视频格式的解决方案吗?@Blam这是一个有效的枚举。如果您看到它确实存在,那么这个特定的方法调用就不会使用它。按照传统的.NET实践,它应该抛出NotSupportedException,而不是默默地失败,但由于它是COM互操作,他们(Microsoft)可能认为这太仓促了,不值得这么做。@AntonKolesnik使用CreateVideo方法代替。仅供参考,我从来没用过这些东西。所有答案都在文档中。您可以尽可能多地阅读(友好)手册。使用PPT中的VBA,SaveAs或SaveCopyAs都可以正常工作。我想知道PPT是否允许您在它仍然开始保存WMV的情况下关闭它。尝试以这种方式保存一个简单的2或3张幻灯片演示文稿,并在App.QuitA预期延迟之前引入30秒左右的延迟。电影文件实际上需要编码,这不是即时操作。=)您好,我已经使用了您的代码,但我在oPres,createVideo(exportName)一行中第一次遇到异常;您可能忘记了什么?感谢您的timeSystem.Runtime.InteropServices.COMException:“演示文稿(未知成员):失败。”