1

I am creating an Windows Form application that can generate Excel files from database data. After filling the database into the file i am opening the it and executing a macro, saving the file and then closing it (or i think i am closing it). These files are saved on my HDD but When all the files are generated i have to be able to generate a new set of files and therefor i have to delete all the old files from my HDD before generating the new set. After running the application once and i try to generate the new set of files, i get an error stating that one of the files (the last file that have been generated in the first batch) is used by another process.

here is my code:

public void RemoveRows_Macro(string fileName)
{

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    xlWorkBook = xlApp.Workbooks.Open(fileName);
    //xlApp.Visible = true;
    xlApp.DisplayAlerts = true;

    //Run the macro
    xlApp.Run("RemoveRows", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    xlWorkBook.Save();

    xlWorkBook.Close(false);
    xlApp.Quit();
    releaseObject(xlApp);
    releaseObject(xlWorkBook);
}

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error in releasing object :" + ex);
        obj = null;
    }
    finally
    {
        GC.Collect();
    }
} 

What am i doing wrong since the application is not releasing the file ?

Lahib
  • 1,305
  • 5
  • 34
  • 62

5 Answers5

2

You would want to release the objects first and then set them as null in the reverse order of how you assigned them.

releaseObject(xlWorkBook);
releaseObject(xlApp);
xlWorkBook = null;
xlApp = null;

I found this link a great resource when I was doing Excel Interop How do I properly clean up Excel interop objects?

Community
  • 1
  • 1
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
1
 finally
{
    GC.Collect(); 
   GC.WaitForPendingFinalizers();
}
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
0

I can't run a test just now, but it looks like you are setting the xlApp and xlWorkBook to null before passing them to releaseObject. There might be other reasons after that.

Joe Stein
  • 31
  • 4
0

You first set the xlApp and xlWorkBook to null and then you try to release it. Try to comment out these two lines:

xlApp = null;
xlWorkBook = null;

Does this fix the problem?

Mitja Bezenšek
  • 2,503
  • 1
  • 14
  • 17
  • Tried that right away. and it didnt fix the problem. it still locks the last generated file – Lahib Mar 19 '13 at 08:18
0

I got the same problem, but I fixed it. So, after I created the file, I close it.

xlWorkbook.SaveAs("nameExcel");
xlWorkbook.Close();
David Buck
  • 3,752
  • 35
  • 31
  • 35
Jeki Gates
  • 1
  • 1
  • 5