PowerPoint中通过C#推出不退出 [英] PowerPoint Launched via C# does not Quit

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

问题描述

嘿,我自动化PowerPoint和Excel从C#WinForms应用程序;我做的是阅读PowerPoint幻灯片,并将其保存在Excel中,然后退出这两个应用程序。 Excel中成功退出,但PPT模板不会退出。问题是,当我转换第一次它不退出,但是当我再次转换它。



下面是我的代码



 
{
PowerPoint.Application ppApp;
PowerPoint.Presentation ppPres;
名单,LT;公司>公司=新的List<公司+ GT;();

ppApp =新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幻灯片= ppPres.Slides.Count;

为(INT幻灯片= 1;滑< =幻灯片,幻灯片++)
{
INT行= 1;
PowerPoint.Cell细胞;
INT形状= 1;

为(;形状< ppPres.Slides [幻灯] .Shapes.Count;形状++)
{
如果(ppPres.Slides [幻灯] .Shapes [外形]。 HasTable == Microsoft.Office.Core.MsoTriState.msoTrue)
{
细胞= ppPres.Slides [幻灯] .Shapes [外形] .Table.Cell(1,1);如果

(cell.Shape.TextFrame.TextRange.Text.Trim()。ToLower将()。包含(实现了))
{
行= ppPres.Slides [幻灯] .Shapes [外形] .Table.Rows.Count;
中断;
}
}
}

公司补偿=新公司(行);
InitializeCompany(REF补偿,ppPres.Slides [幻灯]);
companies.Add(化合物);
}

SaveInExcel(公司);

ppPres.Close();
ppPres = NULL;
ppApp.Quit();
ppApp = NULL;

的回报;
}

赶上(异常前)
{
MessageBox.Show(ex.Message);
}

终于
{
GC.Collect的();
GC.WaitForPendingFinalizers();
}


解决方案

关闭Microsoft Office应用程序似乎棘手得到正确的在第一,但一旦你开展业务的正确顺序,它实际上不是很辛苦的。



释放你的MS Office应用程序可以在两个阶段安全有效地完成:



(1)首先发布所有次要对象,你并不拥有一个名为变量中的引用。你可以通过一个电话这样对 GC.Collect的()然后<一个HREF =htt​​p://www.bing.com/search?setlang=en-US&mkt=en-US&q=gc.waitforpendingfinalizers> GC.WaitForPendingFinalizers()。请注意,如果你正在使用Visual Studio工具为Office(VSTO),那么你需要调用这个对命令两次,以获得COM对象成功释放。您没有使用VSTO,但是,所以叫他们一次就足够了。



(2)然后明确地释放出您在使用调用<通过命名变量持有的对象A HREF你有每个变量=htt​​p://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.finalreleasecomobject.aspx> Marshall.FinalReleaseComObject()。



记住要显式释放的你必须COM组件都变量。如果你错过连一个,那么你的MS Office应用程序将挂起。在代码中,你似乎有抱到您的PowerPoint应用程序的引用三个已命名的变量: ppApp ppPres ,和细胞



考虑到所有这些因素,我认为你的清理应该看起来像以下,这使得使用System.Runtime.InteropServices使用无论是命名空间中或在代码文件的顶部:

 使用System.Runtime.InteropServices // 

//清理:
GC.Collect的();
GC.WaitForPendingFinalizers();

对Marshal.ReleaseComObject(单元);

ppPres.Close();
对Marshal.ReleaseComObject(ppPres);

ppApp.Quit();
对Marshal.ReleaseComObject(ppApp);



试试看吧,我想这应该为你工作?(如果没有,你可能必须表现出更多的代码)的更多信息,我就如何正确释放Excel应用程序在这里详细的解释:



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



希望这可以帮助,让我们知道如何去...



迈克 -


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.

Here is my code

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();
}

解决方案

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.

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

(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) Then explicitly release the objects which you hold via a named variable using a call to Marshall.FinalReleaseComObject() on each variable you have.

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.

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);

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:

How to properly clean up Excel interop objects in C#.

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

-- Mike

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

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