• <bdo id='jlGlY'></bdo><ul id='jlGlY'></ul>
  1. <tfoot id='jlGlY'></tfoot>

    1. <small id='jlGlY'></small><noframes id='jlGlY'>

    2. <legend id='jlGlY'><style id='jlGlY'><dir id='jlGlY'><q id='jlGlY'></q></dir></style></legend>
      <i id='jlGlY'><tr id='jlGlY'><dt id='jlGlY'><q id='jlGlY'><span id='jlGlY'><b id='jlGlY'><form id='jlGlY'><ins id='jlGlY'></ins><ul id='jlGlY'></ul><sub id='jlGlY'></sub></form><legend id='jlGlY'></legend><bdo id='jlGlY'><pre id='jlGlY'><center id='jlGlY'></center></pre></bdo></b><th id='jlGlY'></th></span></q></dt></tr></i><div id='jlGlY'><tfoot id='jlGlY'></tfoot><dl id='jlGlY'><fieldset id='jlGlY'></fieldset></dl></div>
    3. 通过 C# 启动的 PowerPoint 不退出

      PowerPoint Launched via C# does not Quit(通过 C# 启动的 PowerPoint 不退出)
    4. <i id='hm0qe'><tr id='hm0qe'><dt id='hm0qe'><q id='hm0qe'><span id='hm0qe'><b id='hm0qe'><form id='hm0qe'><ins id='hm0qe'></ins><ul id='hm0qe'></ul><sub id='hm0qe'></sub></form><legend id='hm0qe'></legend><bdo id='hm0qe'><pre id='hm0qe'><center id='hm0qe'></center></pre></bdo></b><th id='hm0qe'></th></span></q></dt></tr></i><div id='hm0qe'><tfoot id='hm0qe'></tfoot><dl id='hm0qe'><fieldset id='hm0qe'></fieldset></dl></div>
            <tbody id='hm0qe'></tbody>
            <legend id='hm0qe'><style id='hm0qe'><dir id='hm0qe'><q id='hm0qe'></q></dir></style></legend>
          • <small id='hm0qe'></small><noframes id='hm0qe'>

              <bdo id='hm0qe'></bdo><ul id='hm0qe'></ul>
              <tfoot id='hm0qe'></tfoot>

                本文介绍了通过 C# 启动的 PowerPoint 不退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                嘿,我正在从 C# WinForms 应用程序自动化 PowerPoint 和 Excel;我所做的是从 PowerPoint 中读取幻灯片并将它们保存在 Excel 中,然后退出这两个应用程序.Excel 成功退出,但 PowerPoints 没有退出.问题是当我第一次转换时它不会退出,但是当我再次转换时它会退出.

                Hey I'm automating PowerPoint and Excel from a C# WinForms application; what I do is read slides from PowerPoint and save them in Excel and then quit both apps. Excel quits successfully but PowerPoints doesn't quits. The problem is when I convert first time it doesnt quits, but when I convert again it does.

                这是我的代码

                try
                {
                    PowerPoint.Application ppApp;
                    PowerPoint.Presentation ppPres;
                    List<Company> companies = new List<Company>();
                
                    ppApp = new PowerPoint.Application();
                    ppApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                    ppApp.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMinimized;
                
                    ppPres = ppApp.Presentations.Open(fileTxtBox.Text,
                                                      Microsoft.Office.Core.MsoTriState.msoFalse,
                                                      Microsoft.Office.Core.MsoTriState.msoFalse,
                                                      Microsoft.Office.Core.MsoTriState.msoTrue);
                
                    int slides = ppPres.Slides.Count;
                
                    for (int slide = 1; slide <= slides; slide++)
                    {
                        int rows = 1;
                        PowerPoint.Cell cell;
                        int shape = 1;
                
                        for (; shape < ppPres.Slides[slide].Shapes.Count; shape++)
                        {
                            if (ppPres.Slides[slide].Shapes[shape].HasTable == Microsoft.Office.Core.MsoTriState.msoTrue)
                            {
                                cell = ppPres.Slides[slide].Shapes[shape].Table.Cell(1, 1);
                
                                if (cell.Shape.TextFrame.TextRange.Text.Trim().ToLower().Contains("realized"))
                                {
                                    rows = ppPres.Slides[slide].Shapes[shape].Table.Rows.Count;
                                    break;
                                }
                            }
                        }
                
                        Company comp = new Company(rows);
                        InitializeCompany(ref comp, ppPres.Slides[slide]);
                        companies.Add(comp);
                    }
                
                    SaveInExcel(companies);
                
                    ppPres.Close();
                    ppPres = null;
                    ppApp.Quit();
                    ppApp = null;
                
                    return;
                }
                
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                finally
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                

                推荐答案

                一开始,关闭 Microsoft Office 应用程序似乎很难做到,但一旦确定了正确的操作顺序,实际上一点也不难.

                Shutting down your Microsoft Office application can seem tricky to get right at first, but once you establish the correct order of operations, it's actually not very hard at all.

                可以分两个阶段安全有效地发布您的 MS Office 应用程序:

                Releasing your MS Office application can be done safely and effectively in two stages:

                (1) 首先释放命名变量中没有引用的所有次要对象.您可以通过调用 GC.Collect() 然后 GC.WaitForPendingFinalizers() .请注意,如果您使用的是 Visual Studio Tools for Office (VSTO),则需要调用这对命令两次,以使 COM 对象成功发布.但是,您没有使用 VSTO,因此调用它们一次就足够了.

                (1) First release all the minor objects to which you do not hold a reference within a named variable. You do this via a call to GC.Collect() and then GC.WaitForPendingFinalizers() . Note that if you are using Visual Studio Tools for Office (VSTO), then you need to call this pair of commands twice in order to get the COM objects to successfully release. You are not using VSTO, however, so calling them once is sufficient.

                (2) 然后使用对 Marshall.FinalReleaseComObject() 在您拥有的每个变量上.

                (2) Then explicitly release the objects which you hold via a named variable using a call to Marshall.FinalReleaseComObject() on each variable you have.

                请记住将您拥有的所有变量显式释放到 COM 组件.如果您错过了一个,那么您的 MS Office 应用程序将挂起.在您的代码中,您似乎有三个命名变量包含对您的 PowerPoint 应用程序的引用:ppAppppPrescell.

                Remember to explicitly release all variables that you have to COM components. If you miss even one, then your MS Office application will hang. In your code, you seem to have three named variables that hold a reference to your PowerPoint application: ppApp, ppPres, and cell.

                考虑到这一切,我认为您的清理应该如下所示,它在命名空间内或代码文档顶部使用 using System.Runtime.InteropServices:

                Taking this all into account, I think that your cleanup should look something like the following, which makes use of using System.Runtime.InteropServices either within the namespace or at the top of the code document:

                // using System.Runtime.InteropServices
                
                // Cleanup:
                GC.Collect();
                GC.WaitForPendingFinalizers();
                
                Marshal.ReleaseComObject(cell);
                
                ppPres.Close();
                Marshal.ReleaseComObject(ppPres);
                
                ppApp.Quit();
                Marshal.ReleaseComObject(ppApp);
                

                试一试,我认为这应该适合您...(如果没有,您可能需要显示更多代码.)有关更多信息,我将详细解释如何正确发布 Excel在这里申请:

                Give it a try, I think this should work for you... (If not, you might have to show even more of your code.) For further information, I give a detailed explanation on how to properly release an Excel application here:

                如何正确清理C# 中的 Excel 互操作对象.

                希望这会有所帮助,让我们知道进展如何......

                Hope this helps, let us know how it goes...

                -- 迈克

                这篇关于通过 C# 启动的 PowerPoint 不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                    <legend id='SU25K'><style id='SU25K'><dir id='SU25K'><q id='SU25K'></q></dir></style></legend>
                    • <bdo id='SU25K'></bdo><ul id='SU25K'></ul>

                        <small id='SU25K'></small><noframes id='SU25K'>

                      1. <i id='SU25K'><tr id='SU25K'><dt id='SU25K'><q id='SU25K'><span id='SU25K'><b id='SU25K'><form id='SU25K'><ins id='SU25K'></ins><ul id='SU25K'></ul><sub id='SU25K'></sub></form><legend id='SU25K'></legend><bdo id='SU25K'><pre id='SU25K'><center id='SU25K'></center></pre></bdo></b><th id='SU25K'></th></span></q></dt></tr></i><div id='SU25K'><tfoot id='SU25K'></tfoot><dl id='SU25K'><fieldset id='SU25K'></fieldset></dl></div>
                            <tbody id='SU25K'></tbody>
                          <tfoot id='SU25K'></tfoot>