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 98473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:12:10+00:00 2026-05-11T00:12:10+00:00

Possible Duplicate: How to properly clean up Excel interop objects in C# I’ve read

  • 0

Possible Duplicate:
How to properly clean up Excel interop objects in C#

I’ve read many of the other threads here about managing COM references while using the .Net-Excel interop to make sure the Excel process exits correctly upon exit, and so far the techniques have been working very well, but I recently came across a problem when adding new worksheets to an existing workbook file.

The code below leaves a zombie Excel process.

If I add a worksheet to a newly created workbook file, it exits fine. If I run the code excluding the .Add() line, it exits fine. (The existing file I’m reading from is an empty file created by the commented out code)

Any ideas?

//using Excel = Microsoft.Office.Interop.Excel; //using System.Runtime.InteropServices; public static void AddTest() {   string filename = @'C:\addtest.xls';   object m = Type.Missing;   Excel.Application excelapp = new Excel.Application();   if (excelapp == null) throw new Exception('Can't start Excel');   Excel.Workbooks wbs = excelapp.Workbooks;    //if I create a new file and then add a worksheet,   //it will exit normally (i.e. if you uncomment the next two lines   //and comment out the .Open() line below):   //Excel.Workbook wb = wbs.Add(Excel.XlWBATemplate.xlWBATWorksheet);   //wb.SaveAs(filename, m, m, m, m, m,    //          Excel.XlSaveAsAccessMode.xlExclusive,   //          m, m, m, m, m);    //but if I open an existing file and add a worksheet,   //it won't exit (leaves zombie excel processes)   Excel.Workbook wb = wbs.Open(filename,                                m, m, m, m, m, m,                                Excel.XlPlatform.xlWindows,                                m, m, m, m, m, m, m);    Excel.Sheets sheets = wb.Worksheets;    //This is the offending line:   Excel.Worksheet wsnew = sheets.Add(m, m, m, m) as Excel.Worksheet;     //N.B. it doesn't help if I try specifying the parameters in Add() above    wb.Save();   wb.Close(m, m, m);    //overkill to do GC so many times, but shows that doesn't fix it   GC();   //cleanup COM references   //changing these all to FinalReleaseComObject doesn't help either   while (Marshal.ReleaseComObject(wsnew) > 0) { }    wsnew = null;   while (Marshal.ReleaseComObject(sheets) > 0) { }   sheets = null;   while (Marshal.ReleaseComObject(wb) > 0) { }   wb = null;   while (Marshal.ReleaseComObject(wbs) > 0) { }   wbs = null;   GC();   excelapp.Quit();   while (Marshal.ReleaseComObject(excelapp) > 0) { }   excelapp = null;   GC(); }  public static void GC() {   System.GC.Collect();   System.GC.WaitForPendingFinalizers();   System.GC.Collect();   System.GC.WaitForPendingFinalizers(); } 
  • 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. 2026-05-11T00:12:11+00:00Added an answer on May 11, 2026 at 12:12 am

    I don’t have the code to hand, but I did run into a similar problem. If I recall correctly, I ended up retrieving the process id of the excel instance, and killing it (after a suitable wait period, and when the other method failed).

    I think I used:

    GetWindowThreadProcessId (via P/Invoke) on the excel object hwnd property to get the process id, and then used Process.GetProcessById to get a process object. Once I’d done that, I’d call Kill on the process.

    EDIT: I have to admit, this isn’t the ideal solution, but if you can’t find the rogue interface that isn’t being released, then this will fix it in true eggshell/sledgehammer fashion. 😉

    EDIT2: You don’t have to call Kill on the process object immediately… You could first try calling Close before resorting to Kill.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What about <a href="SomeFunction(this);">Href works here</a> function SomeFunction(context) { var… May 11, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer These are the items that I consider for the topic… May 11, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer <% if (Html.IsCurrentAction("Index", "Home")) { %> <li class="active"><%= Html.ActionLink("Home", "Index",… May 11, 2026 at 9:16 pm

Related Questions

Possible Duplicate: How to properly clean up Excel interop objects in C# I've read
I am trying to write the HTML and CSS for an internal webapp properly.
Possible Duplicate: How can I properly handle 404 in ASP.NET MVC? I've made the
Possible Duplicate: How to collapse a row in RDLC rather than just hide it?
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.