Not able to create object of Excel.Application

2

In my C# application i am importing and exporting data to excel. I have office 2013.

I am using following code:

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application _excelApp = null;
_excelApp = new Excel.Application();

This code was working fine but recently i installed Microsoft project professional 2013, it also update office. After this i am getting error in _excelApp = new Excel.Application();

Error is:

Unable to cast COM object of type 'System.__ComObject' 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: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

c#
.net
excel
interop
asked on Stack Overflow May 18, 2015 by Deepak gupta • edited Sep 15, 2015 by pnuts

3 Answers

0

@Deepak gupta, i can t comment so i post it here. Maybe Soner is right. I used to have the same problem so i decided to use the old way to open an Excel file (with an oledb command).

What do you need? Read an Excel or save a new one?

For example in my case i needed to read an Excel (that was and .xls or .xlsx):

               //Check whether file extension is xls or xslx
            if (fileExtension == ".xls")
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            else if (fileExtension == ".xlsx")
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

            //Create OleDB Connection and OleDb Command            
            OleDbCommand cmd = new OleDbCommand();
            OleDbConnection con = new OleDbConnection(connectionString);

            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;

            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();

            //send query, send query, fill adapter:
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "A1:GR255] WHERE [0] IS NOT NULL ";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            con.Close();

Or maybe you can try using this library ExcelLibrary that is described in this post Create Excel (.XLS and .XLSX) file from C#.

answered on Stack Overflow May 18, 2015 by paulofer85 • edited May 23, 2017 by Community
0

This is way to create Object and thereby to copy the worksheet from One excel to another Excel

Dim CopyFrom As Object Dim CopyTo As Object Dim CopyThis As Object Dim xl As Object

    xl = CreateObject("Excel.Application")
    xl.Visible = False
    CopyFrom = xl.Workbooks.Open("E:\EXCEL\From.xls")
    CopyTo = xl.Workbooks.Open("E:\EXCEL\To.xls")
    For i = 0 To 1
        ''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
        If i = 0 Then
            CopyThis = CopyFrom.Sheets(1)
            CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
            CopyTo.Sheets(3).Name = "Sheet3"
        Else
            CopyThis = CopyFrom.Sheets(2)
            CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
            CopyTo.Sheets(4).Name = "Sheet4"
        End If
    Next
    CopyTo.Sheets(1).Activate()
    CopyTo.Save()
    'CopyTo.SaveAs("E:\EXCEL\Check.xls")
    xl.Quit()
answered on Stack Overflow Jun 15, 2015 by Pavithran
0

The problem has been resolved. When i installed Microsoft project professional 2013, it also update microsoft office, but the updation of office was not successful.

When i repair office, it started working fine.

answered on Stack Overflow Oct 16, 2015 by Deepak gupta

User contributions licensed under CC BY-SA 3.0