System.Runtime.InteropServices.COMException(0x80080005)

时间:2013-06-03 13:40:33

标签: c# .net com powerpoint office-automation

我收到错误:

  

System.Runtime.InteropServices.COMException(0x80080005):由于以下错误,检索具有CLSID {91493441-5A91-11CF-8700-00AA0060263B}的组件的COM类工厂失败:80080005。

代码行PowerPoint.Application PowerPoint_App = new PowerPoint.Application();

这个代码块在这里:

using (new Impersonator(Installs.Current.PPTUser, null, Installs.Current.PPTPassword))
{
    PowerPoint.Application PowerPoint_App = new PowerPoint.Application();
    PowerPoint.Presentation presentation = null;
    try
    {
        PowerPoint_App.Visible = MsoTriState.msoTrue;
        presentation = PowerPoint_App.Presentations.Open(
            strPptFilePath, Microsoft.Office.Core.MsoTriState.msoFalse,
            Microsoft.Office.Core.MsoTriState.msoFalse,
            Microsoft.Office.Core.MsoTriState.msoTrue);

        for (int i = 0; i < presentation.Slides.Count; i++)
        {
            readSlides(presentation, i);
        }
        presentation.Close();
        PowerPoint_App.Quit();
    }
    catch (Exception ex)
    {
        strSuccess = ex.ToString();
        MindMatrix.Libraries.Entities.ExceptionMessage.HandleException(ex, null);
    }
    finally
    {
        Marshal.FinalReleaseComObject(presentation);
        Marshal.FinalReleaseComObject(PowerPoint_App);
    }
}

每当我第一次运行代码时,它都可以正常运行,但随后它会为PowerPoint创建进程(可以在任务管理器中看到)。我已经使用PowerPoint_App.Quit();退出已经打开的进程,但是它没有工作并且让我犯错误。我转到任务管理器并从那里结束进程,然后它就可以再工作一次了。

从代码退出流程时我做错了什么或者还有其他办法吗?

2 个答案:

答案 0 :(得分:2)

好吧,就是这样。我提到了所有可能的解决方案。来自杀戮,戒烟,关闭,Marshal.FinalReleaseComObject,GC Collect和WaitForPendingFinalizers。但没有什么对我有用。所以我发现了正在运行的进程并通过代码将其杀死。而bamn一切都按我预期的方式运作。

<强>代码:

  Process[] pros = Process.GetProcesses();
    for (int i = 0; i < pros.Count(); i++)
         {
           if (pros[i].ProcessName.ToLower().Contains("powerpnt"))
                {
                  pros[i].Kill();
                }
         }

名称空间必填:

System.Diagnostics

答案 1 :(得分:0)

请参阅Application.Quit() method failing to clear process

还有一个选项,让您的应用程序检查exe是否正在运行,尝试在您的finally中手动终止它,但我强烈建议不要这样做。

相关问题