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