日期:2014-05-18  浏览次数:21162 次

PowerPoint二次开发-无法关闭进程
Microsoft.Office.Interop.PowerPoint.ApplicationClass appClass = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

Microsoft.Office.Interop.PowerPoint.Presentation pres = pptApp.appClass.Presentations.Open(FileName,Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

pres.Close();
appClass.Quit();

结果PowerPoint进程仍然存在,并且被打开的ppt文件无法打开,只有把进程终止后,才能打开。

------解决方案--------------------
一般应用Office类的,只能先取它的当前id,通过id进行进程的Kill完成终止
先取出所有PowerPoint的进程,并将它们的进程id以列表或者其他方式保存,在调用的时候,再取一次,将当前进程的id取出.然后通过id完成进程的终止.
------解决方案--------------------
有没有试过把接口释放掉。。。
例如你把接口放在某个类中的成员变量中,你就把这个类释放或者成员变量置空。然后执行垃圾回收。

因为在Office的OLE操作中,COM服务器会维护接口的引用计数,用_AddRef和_Release来维护,映射到高级语言中则变成了,你将一个接口引用赋值时会自动调用_AddRef和_Release,而直到COM直到你一个接口引用都没有的时候才会释放服务器资源。

具体我没有试,只是说有可能是这个原因。
------解决方案--------------------
C# code
PowerPoint.ApplicationClass appClass = new PowerPoint.ApplicationClass();
            appClass.Presentations.Open("F:\\a.ppt", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
            appClass.Quit();
            GC.Collect();