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