Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:05:27+00:00 2026-06-10T04:05:27+00:00

Ah, another Excel not releasing post on SO….. I have an Excel workbook which

  • 0

Ah, another “Excel not releasing” post on SO…..

I have an Excel workbook which I load through Excel (2010), invoke the Excel application’s Run method to launch a macro in the workbook, save the updated workbook and quit Excel.

And Excel stays behind, even after the calling app is closed.

I haven’t fallen into the “two dots” trap, I close all that can be closed, quit all that can be quit and release every reference to every COM object used and given the GC a kick.

If I don’t make the call to run the macro, all is well. So something about this particular macro is at fault, as other workbooks don’t show this behaviour.

The macro itself loads the Solver add-in and makes some calls to C++ functions in a non .NET win32 dll. It invokes the Solver add-in, and does nothing else out of the ordinary (math, set ranges, set cell values and the like).

Are there any Solver or macro related gotchas I should know about? I’ve even tried unloading the add-in from C# – no joy.

    static void NAR(object o) // MSDN article suggestion.
    {
        try
        {
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
            {
            }
        }
        catch
        {
            // swallow
        }
        finally
        {
            o = null;
        }
    }
    static void Excel(string FilePath, string SaveAsPath)
    {
        Application excelApplication = null;
        Workbooks workbooks = null;
        Workbook workbook = null;

        try
        {
            excelApplication = new Application { DisplayAlerts = false, Visible = false };
            workbooks = excelApplication.Workbooks;
            workbook = workbooks.Open(FilePath);

            excelApplication.Run("SolveEquationWithSolver");

            workbook.CheckCompatibility = false;
            workbook.SaveAs(SaveAsPath);
        }
        finally
        {
            if (workbook != null)
            {
                workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlDoNotSaveChanges, Type.Missing, Type.Missing);
                NAR(workbook);
            }

            if (workbooks != null)
            {
                workbooks.Close();
                NAR(workbooks);
            }

            if (excelApplication != null)
            {
                excelApplication.Quit();
                NAR(excelApplication);
            }

            // Kick GC, as suggested elsewhere on SO. Not my idea.
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Double-tap GC, as suggested elsewhere on SO. Not my idea.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

UPDATE

Another forum has suggested using excelApplication.ActiveWindow.Close(), and this works!! (if the workbook.Close() call is removed).

However, this breaks the “two dots” rule. So, what is it doing that means we can get away with it in this case, but not in the general case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:05:29+00:00Added an answer on June 10, 2026 at 4:05 am

    I had similar problem to yours at one point, and eventually gave up and decided to just kill Excel. If you close the workbooks involved before killing the process, you should not have any annoying side effects (like “recovered” workbooks next time you open Excel).

    using System.Runtime.InteropServices;
    
    // ...
    // Inside a class:
    
    [DllImport("user32.dll")]
    private static extern uint GetWindowThreadProcessId(
        IntPtr hWnd, out uint lpdwProcessId);
    
    // Returns "true" on success, "false" on failure.
    public static bool KillExcel(Application excel) {
        uint processId;
        GetWindowThreadProcessId((IntPtr)excel.Hwnd, out processID);
        try {
            Process.GetProcessById((int)processID).Kill()
    
        } catch (Exception /* TODO: catch only relevant exceptions */) {
            return false;
        }
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel 2010 macro application that is made up of several workbook
This question stems off another post I had. (see Search through column in excel
Hi All, I have an application (windows service/C#) which starts EXCEL and POWERPOINT and
We have an Excel workbook which has c# VSTO code in it and two
I basically have a program that filters records from one excel file to another
I would like to copy a macro from one Excel workbook to another using
I have an Excel file that I am trying to load into R using
I write an add-in *.xlam for excel workbook. I have tested it on my
I have a c# tool that creates excel worksheets, which will be later read
I'm looking for an Excel library which reads/writes .xls (not .xlsx) files. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.