嘿,我正在从 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 应用程序的引用:ppApp
、ppPres
和 cell
.
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 不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!