无法使用office interop C#关闭powerpoint应用程序 [英] Unable to close powerpoint application using office interop C#

查看:378
本文介绍了无法使用office interop C#关闭powerpoint应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我们正在winform应用程序中自动化MsOffice(C#)。



目前我们正在努力创建一个powerpoint文件(以编程方式)。我们成功创建了pptx文件。



创建pptx文件后,我们试图关闭演示文稿和应用程序。它仅仅是演示文稿,但不是ppt应用程序。



MyCode



我尝试了什么:



  private   static   void  QuitApplication(PowerPointInterop.Application powerPointApplication)
{
if (powerPointApplication!= null
{
try
{
powerPointApplication.DisplayAlerts = PowerPointInterop.PpAlertLevel.ppAlertsNone;
powerPointApplication.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
powerPointApplication.Quit();

Marshal.FinalReleaseComObject(powerPointApplication.Presentations);
Marshal.FinalReleaseComObject(powerPointApplication);

powerPointApplication = null ;

GC.Collect();
GC.WaitForPendingFinalizers();

// 需要调用GC两次才能获得名为
// - 第一次,它只是列出了什么是
// 已完成,第二次,它实际上正在最终确定。只有
// 然后对象会自动执行ReleaseComObject。
GC.Collect();
}
catch (例外)
{
// 忽略
}
}
}





  public   static   bool  CreateNewFile(IQuestion问题,字符串文件名)
{
WinUtil.DeleteFile(filename);
if (File.Exists(filename))
return ;

PowerPointInterop.Application powerpointApplication = null ;
尝试
{
// 在初始化时避免屏幕闪烁或不需要的警报
powerpointApplication = new PowerPointInterop.Application();

// 创建演示文稿
PowerPointInterop.Presentation pptPresentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue);

pptPresentation.SaveAs(filename,PowerPointInterop.PpSaveAsFileType.ppSaveAsDefault,MsoTriState.msoTriStateMixed);

pptPresentation.Close();
powerpointApplication.Quit();

return true ;
}
catch (例外)
{
return false ;
}
最后
{
QuitApplication(powerpointApplication);
}
}





参考: Office C#app自动化C#for Visual Studio 2010中的PowerPoint(CSAutomatePowerPoint)示例 [ ^ ]



有人可以帮助我。



谢谢

解决方案

我建​​议你阅读: Office应用程序在从Visual Studio .NET客户端自动化后不会退出 [ ^ ]

您必须使用Marshal.ReleaseComObject Method(Object)(System.Runtime.InteropServices) [ ^ ]直接在您的ppt应用程序对象上(实例)。

Dear All,

We are automating MsOffice in winform application(C#).

Currently we are working on, to create a powerpoint file(Programmatically). We are successfully created pptx file.

After creating a pptx file, we are trying to close both the presentation and application. Its clsoing only presentation, but not ppt application.

MyCode

What I have tried:

private static void QuitApplication(PowerPointInterop.Application powerPointApplication)
        {
            if (powerPointApplication != null)
            {
                try
                {
                    powerPointApplication.DisplayAlerts = PowerPointInterop.PpAlertLevel.ppAlertsNone;
                    powerPointApplication.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
                    powerPointApplication.Quit();

                    Marshal.FinalReleaseComObject(powerPointApplication.Presentations);
                    Marshal.FinalReleaseComObject(powerPointApplication);

                    powerPointApplication = null;

                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    // GC needs to be called twice in order to get the Finalizers called  
                    // - the first time in, it simply makes a list of what is to be  
                    // finalized, the second time in, it actually is finalizing. Only  
                    // then will the object do its automatic ReleaseComObject.
                    GC.Collect();
                }
                catch (Exception)
                {
                    //Ignore
                }
            }
        }



public static bool CreateNewFile(IQuestion question, string filename)
        {
            WinUtil.DeleteFile(filename);
            if (File.Exists(filename))
                return false;

            PowerPointInterop.Application powerpointApplication = null;
            try
            {
                //Avoid screen flickering or unwanted alerts while initializing
                powerpointApplication = new PowerPointInterop.Application();

                // Create the Presentation File
                PowerPointInterop.Presentation pptPresentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue);               

                pptPresentation.SaveAs(filename, PowerPointInterop.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTriStateMixed);

                pptPresentation.Close();
                powerpointApplication.Quit();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                QuitApplication(powerpointApplication);
            }
        }



Ref: Office C# app automates PowerPoint (CSAutomatePowerPoint) sample in C# for Visual Studio 2010[^]

can anyone please help me.

Thanks

解决方案

I'd suggest to read this: Office application does not quit after automation from Visual Studio .NET client[^]
You have to "clean up" by using Marshal.ReleaseComObject Method (Object) (System.Runtime.InteropServices)[^] directly on your ppt application object (instance).


这篇关于无法使用office interop C#关闭powerpoint应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆