Error in Opening the Excel file from C#.Net (VS2010)

1

I am trying to open an excel file (.xlsx) using C# (VS2010 professional) code. I am getting an (untraceable, to me) exception when executing/single stepping the last 2 lines of the below code. Below mentioned is my code for opening an existing excel file.

        string tesfile = "C:\\Users\\AWaheed3\\Desktop\\1.xlsx";
        Microsoft.Office.Interop.Excel.Application xlApp;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlApp.Visible = true;
        xlWorkBook = xlApp.Workbooks.Open(tesfile, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);   

Also I have included this below line at the start of the code. Further more I have Added the reference of the Microsoft.Office.Interop.Excel from Project->Add Reference (.NET Tab)

 using Microsoft.Office.Interop.Excel;

Can anybody advise why my code is failing/throwing an error?

Regards Asad

EDITED***************************

Here is the message/error I am receiving. Note that the code is failing even while executing the xlApp.Visible = ture line. Error is

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).


c#
excel
asked on Stack Overflow Jun 4, 2013 by Asad Waheed • edited Jun 4, 2013 by Asad Waheed

1 Answer

0

Try to remove the double backslash and write instead \ or you can do is

Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
try
{
     excel = new Microsoft.Office.Interop.Excel.Application();
     object missing = Type.Missing;
     object trueObject = true;
     try
     {
          excel.Visible = false; // or true
          excel.DisplayAlerts = false;
     }
     catch(Exception e)
     {
                Console.WriteLine("-------Error hiding the application-------");
                Console.WriteLine("Occured error might be: " + e.StackTrace);
     }
     xls = excel.Workbooks.Open(excelFile, missing, trueObject,    missing,missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
     //xls = excel.Workbooks.Open(@"file.xls", missing, trueObject,    missing,missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
catch (Exception ex)
{
            MessageBox.Show("Error accessing Excel document.\n\n" +
            ex.Message);
}
// Must be surrounded by try catch to work.
// http://naimishpandya.wordpress.com/2010/12/31/hide-power-point-application-window-in-net-office-automation/

The @ removes any issue with the path. Code is similar to yours of course. Just I'm rewriting it in different way. Look for the System.Reflection, I'm using System.Type instead. And some advice for the future, if you work with COM files, try to close them correctly at the end. **Edited.

answered on Stack Overflow Jun 4, 2013 by mike27015 • edited Jun 4, 2013 by mike27015

User contributions licensed under CC BY-SA 3.0