Exemplo n.º 1
0
        public void Kill()
        {
            Int32 ExcelHwnd = excel.Hwnd;

            Process[] localExcel = Process.GetProcessesByName("EXCEL");
            foreach (Process Pgm in localExcel)
            {
                // xlMinimized keeps the screen from flashing when the user interface is made visible to set the MainWindowHandle
                try
                {
                    excel.WindowState = XlWindowState.xlMinimized;
                }
                catch (Exception e1)
                {
                    // Skip it
                    catchData = "No window to minimize - excel.Kill: " + path + e1.Message;
                }
                // Excel won't have an Hwnd (window Handle) until it's visible.
                excel.Visible = true;
                if ((Pgm.ProcessName == "EXCEL") && (ExcelHwnd == Pgm.MainWindowHandle.ToInt32()))
                {
                    Pgm.Kill();
                }
            }
        }