DDuffy
DDuffy

Reputation: 413

Shutdown program without closing separate instances of excel

I am writing a program that works heavily with Excel. I seem to have hit a snag when executing my Exit command. Not only will it close all instances of excel that it created (Which was the point in case anything was left hanging), but it will also close down any instances of excel that were opened manually outwith the program.

Would anyone be able to point out where I am going wrong with this?

(Also, I am learning C# on the fly, so please excuse any "Common" mistakes. All Constructive criticism is welcome.)

        private void BtnExit_Click_1(object sender, EventArgs e)
    {
        try
        {
            if (ObjApp == null)
            {
                Excel.Application ObjApp = new Excel.Application();
            }

            Modules.MessageUpdate(this, ObjApp, EH, 5, 22, "", "", "", 0, 0, 0, 0, "Application Quit.", "N");

            ObjApp.Quit();

            if (ObjApp != null)
            {
                ObjApp = null;
            }
            if (UC != null)
            {
                UC = null;
            }
            if (Zoho != null)
            {
                Zoho = null;
            }
            if (Modules != null)
            {
                Modules = null;
            }
            if (EH != null)
            {
                EH = null;
            }
            if (proc != null)
            {
                proc = null;
            }
            if (Wait != null)
            {
                Wait.Close();
                Wait = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Environment.Exit(0);
        }
        catch(COMException)
        {
            //System.Windows.Forms.Application.Restart();
            if (ObjApp != null)
            {
                ObjApp = null;
            }
            if (UC != null)
            {
                UC = null;
            }
            if (Zoho != null)
            {
                Zoho = null;
            }
            if (Modules != null)
            {
                Modules = null;
            }
            if (EH != null)
            {
                EH = null;
            }
            if (proc != null)
            {
                proc = null;
            }
            if (Wait != null)
            {
                Wait.Close();
                Wait = null;
            }
            //ObjApp.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Environment.Exit(0);
        }
        catch
        {
            if (ObjApp != null)
            {
                ObjApp = null;
            }
            if (UC != null)
            {
                UC = null;
            }
            if (Zoho != null)
            {
                Zoho = null;
            }
            if (Modules != null)
            {
                Modules = null;
            }
            if (EH != null)
            {
                EH = null;
            }
            if (proc != null)
            {
                proc = null;
            }
            if (Wait != null)
            {
                Wait.Close();
                Wait = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Environment.Exit(0);

        }
    }

Upvotes: 0

Views: 37

Answers (1)

Murray Foxcroft
Murray Foxcroft

Reputation: 13745

Keep a list of the Workbooks your application creates, then have your "exit" just close those Workbooks. If the last workbook closes and Excel has 0 remaining workbooks, then close Excel too, otherwise leave it open with the manual workbooks still available.

Upvotes: 1

Related Questions