PowerPoint挂在Application.Quit上

时间:2012-07-11 22:12:41

标签: com powerpoint

这是我正在运行的代码:

MsoTriState readOnly = MsoTriState.msoTrue;
MsoTriState untitled = MsoTriState.msoFalse;
MsoTriState withWindow = MsoTriState.msoFalse;
string filePath; //some file path to a pptx

ppt.Application app = null;
ppt.Presentations presentations = null;
ppt.Presentation presentation = null;

app = new ppt.Application();
presentations = app.Presentations;
presentation = presentations.Open(filePath, readOnly, untitled, withWindow);

System.Runtime.InteropServices.Marshal.ReleaseComObject(presentation);
presentation = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(presentations);
presentations = null;

app.Quit(); //HANGING HERE

System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;

这是一个非常基本的代码,可以打开PowerPoint应用程序然后关闭它。但是,出于某种原因,它挂在app.Quit()电话上。另外,我不知道为什么,但如果我注释掉我在ComRelease演示文稿中的行,它就不会挂起。另一种方法是在调用app.Quit之前制作app.Visible = msoTrue。

有谁知道这里发生了什么?我搞乱了我的ComRelease程序吗?

1 个答案:

答案 0 :(得分:0)

我在这里为你锻炼。你可以找到正在运行的过程并通过代码杀死它。

<强>代码:

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