private static void CheckIfLast(Excel.Workbook Wb, ref bool Cancel) { try { Excel.Workbooks wbs = _xlApp.Workbooks; int count = wbs.OfType <Excel.Workbook>() .Count(wb => !wb.Equals(Wb) && wb.Windows[1].Visible); if (count <= 1) { _xlApp.Quit(); Marshal.ReleaseComObject(_xlApp); _xlApp = null; } } catch { } }