1

I am trying to open an excel file through open file dialog but I am getting the following error

enter image description here

this the code i'v written, lemme know where it goes wrong

Excel.Application excelApp = new Excel.Application();
Excel.Workbook newWorkbook = excelApp.Workbooks.Add();  
Excel.Workbook excelWorkbook = null;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files |*.xlsx";
ofd.InitialDirectory = @"C:\";
if (ofd.ShowDialog() == DialogResult.OK)
{
    string path = System.IO.Path.GetFullPath(ofd.FileName);
    try
    {
        excelWorkbook = excelApp.Workbooks.Open(path,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);
    }
    catch (Exception theException)
    {
        String errorMessage;
        errorMessage = "Error: ";
        errorMessage = String.Concat(errorMessage, theException.Message);
        errorMessage = String.Concat(errorMessage, " Line: ");
        errorMessage = String.Concat(errorMessage, theException.Source);
        MessageBox.Show(errorMessage, "Error");
    }
}

I am doing this coz I need to get values from the excel sheet. Please let me know if you require more details.

Edit -: when I looked closely I understood that the message box is not prompted for the first trial of opening the file, but the file does not open and then the message box appears during every successive trial of opening it. As suggested by @Pankaj, I tried adding Finally at the bottom but I still get the message box after 2nd trial of opening the file.

3
  • Do you have by any chance that same file already opened in Excel? Commented Sep 3, 2013 at 11:57
  • Nope, I do not have that file open Commented Sep 3, 2013 at 11:58
  • While not solving the current problem I thought I'd give a tip about closedxml.codeplex.com which I've always found easier then to actually work with Excel. Commented Sep 3, 2013 at 12:56

3 Answers 3

1

Well, a missing line in the code was -: excelApp.Visible = true;

the code now looks like-:

excelWorkbook = excelApp.Workbooks.Open(path,
            0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);
// do the operations on file
.
.
// open the file here 
excelApp.Visible = true;

As suggested by @Pankaj, I applied

finally
{
    excelWorkbook.Close();
    excelApp.Application.Quit();
    Marshal.ReleaseComObject(excelApp);
}

but the problem with this is that the file closes immediately as we close workbook and excel application and I do not want the file to close as I would perform operations and user would then view the excel file. Also, closing application and workbook throws an unhandeled NullReferenceException if the user does not choose any file and closes the file dialogue. So I improved the the block and it now looks like

finally
{
    Marshal.ReleaseComObject(excelApp);
}

There is just one catch though, a new file along with the selected file is opened, its not a problem as of now but I am working on closing the newly opened file. thank you for your answeres @pankaj and @Csaba. :)

Sign up to request clarification or add additional context in comments.

2 Comments

@Vim, If you think my answer was useful, Please upvote our answers, that is StackOverflow method of saying Thanks. :)
@Pankaj Yes, if I think your answer is useful I will surely do that! :) and that would even be my method of saying thanks.. ;)
0

You should close your file and quit excel once you are done with that, so Add this finally thing in the end of your code

try
{
    excelWorkbook = excelApp.Workbooks.Open(path,
    0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
    true, false, 0, true, false, false);
    // ... Do all your processing with excel file here
}
catch (Exception theException)
{
    String errorMessage;
    errorMessage = "Error: ";
    errorMessage = String.Concat(errorMessage, theException.Message);
    errorMessage = String.Concat(errorMessage, " Line: ");
    errorMessage = String.Concat(errorMessage, theException.Source);
    MessageBox.Show(errorMessage, "Error");
}
finally
{
   excelWorkbook.Close();
   excelApp.Application.Quit();
   Marshal.ReleaseComObject(excelApp);  
}

For the first time, this error is not there, This error will appear when you are reading the file next time.

4 Comments

Does not work, still the same error, the error message box is shown while the point of execution' is in the try block so I don't think adding finally would help.
Kill all the "Excel" processes from Task Managers, and then try it, It should work , Excel is quite stubborn about terminating its processes. I just tried it and it works.
hmmm tried that but no luck, my file does not open at all.. is there something wrong with excelApp.Workbooks.Open(path,0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
@Vin, this is correct, if it would have been wrong, the file wouldnt have opened in the first iteration, the problem is it didnt got closed properly. Did you tried adding this Marshal.ReleaseComObject(excelApp);
0

Try supplying true as a third parameter for Workbooks.Open. MSDN: http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.workbooks.open.aspx

ReadOnly
    Type: System.Object
    Optional Object. True to open the workbook in read-only mode.

You only want to get values so opening it read-only should be enough for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.