Hey everyone,

I'm having an issue where my application will keep Excel running even after it closes. I think I'm closing everything properly, but is there a way to check on close?

Here's part of my code:

////////////// Part of ExcelWriter class
public void CreateSheet(Laptop lptp, string file, string sheet)
        {
            try
            {
                xlBook = xl.Workbooks.Open(file_path + file, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                xlSheet = xlBook.Worksheets.get_Item(1);
                xl.Worksheets.Add(oMissing, oMissing, oMissing, oMissing);
                xl.Worksheets[1].name = sheet;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Create Error:  " + ex.Message, "Could not create sheet", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xlBook.Save();
                xl.Application.Workbooks.Close();
                xl.Workbooks.Close();
            }
        }

/////////////// Part of ExcelWriter class.
public void UpdateFile(Laptop lptp, string file)
        {
            try
            {
                xlBook = xl.Workbooks.Open(file_path + file, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                xlSheet = xlBook.Worksheets.get_Item(1);
                xl.ActiveSheet.Name = "List";
                int num_rows = xlSheet.Rows.Count;
                xlSheet.Cells[1, num_rows+1] = "Test";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Write Error:  " + ex.Message, "Could not write to file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xlBook.Save();
                xl.Application.Workbooks.Close();
                xl.Workbooks.Close();
            }
        }

Recommended Answers

All 5 Replies

Ok, so my problem seems to be occurring only when I have an exception thrown. But I would think the finally{...} block would still take care of that. Still working on a solution...

Thanks for any help

Another piece to the puzzle - I'm getting a lot of tmp files (attachment). I've also noticed that any time the program errors out (and sometimes when it closes fine), I still have numerous instances of EXCEL.EXE in task manager. What am I doing wrong?

Re lots of excel processes.
Maybe you should try to use only one excel app and open each workbook using that one only.
At least you will only have one open process to worry about.

Re-not closing:
Your thread about unreachable code had this in it

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);

Did this fix the problem? If so please mark this thread solved.

I noticed you use xlBook = xl.Workbooks.Open ; i'm certain i read somewhere that a general rule of thumb is two avoid "double dots" when using COM objects.
When .net calls the com objects it creates a wrapper which ends up something like:

WorkBook xlBook;
WorkBooks tmp;
tmp = xlApp.Workbooks;
xlBook = tmp.Open("yourfile");

So when you call Marshal.ReleaseComObject on the objects you have created, you arent clearing out those temp ones that the wrapper leaves behind.
If you rewrite yours to look like this,manually breaking them down and creating the 'tmp' objects yourself, you can then call the Marshal to release them.

Hope this helps :)

Thanks for the input guys - been driving me crazy.

nick> I only have (it seems) one EXCEL.EXE after each run of the program - sometimes two since I'm actually modifying two separate files. Also, I added the Marshal.Release shortly after I posted this code - didn't seem to help much.

Ryshad> I had no clue about the tmp files - I'll give it a whirl. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.