Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Interop Excel and my code works fine

    class GeneralData
{   public static string excelPath = System.Windows.Forms.Application.StartupPath + @"\SteamProp";
    public static ExcelApp._Application oExcel;
    static ExcelApp.Workbooks oBooks;
    public static ExcelApp._Workbook oBook;
    static object oMissing = System.Reflection.Missing.Value;

    // Function will be used in Main Form Load Method
    public static void OpenExcelConnection()
    {
        oExcel = new ExcelApp.Application();
        oExcel.Visible = false;
        oBooks = oExcel.Workbooks;
        oBook = oBooks.Open(excelPath, oMissing, oMissing,
            oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);            
    }


    public static void ReleaseExcel()
    {
        try
        {
            oBook.Close(true, excelPath, oMissing);
            oExcel.Quit();

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBook);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBooks);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oExcel);
            oBook = null;
            oBooks = null;
            oExcel = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        catch
        { }
    }

I made (OExcel) static to use it from different forms. I open the (OExcel) and use it then close at the end by calling the ReleaseExcel() Method. The code work fine, but if there was any different excel file opened from outside the application, the file which used in the C# application appears to the user even that :

oExcel.Visible = false;

and when the ReleaseExcel() method called this file doesn't close, and if i closed the C# application and opened it again the next Exception is raised:

The Microsoft Jet database engine cannot open the file ". It is already opened exclusively by another user, or you need Permission to view its data.

So Could you help me.

share|improve this question

1 Answer 1

When you done disposing the object, use

GC.Collect();

This is how I dispose my Excel object

        private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
share|improve this answer
    
Thanks, but not what i need. –  Ahmed Noaman Jun 12 '13 at 2:20
    
Seems like you still have your objects open (check your task manager); this should help you to close them and therefore ready to be re-opened again? –  Andrew Jun 12 '13 at 2:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.