1
votes

I am trying to write data to Excel from c#, but while debugging I have an exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: Интерфейс не поддерживается (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
   class Program
  {
    static void Main(string[] args)
    {
        Excel.Application excelapp;
        Excel.Window excelWindow;
        Excel.Workbooks excelappworkbooks;
        Excel.Workbook excelappworkbook;
        Excel.Sheets excelsheets;
        Excel.Worksheet excelworksheet;
        Excel.Range excelcells;
        excelapp = new Excel.Application();
        excelapp.Visible = true;

        excelappworkbooks = excelapp.Workbooks;

        excelappworkbook = excelapp.Workbooks.Open(@"C:\Documents and Settings\дима\Мои документы\Visual Studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\bin\Debug\a.xls",
                           Type.Missing, Type.Missing, Type.Missing,
         "WWWWW", "WWWWW", Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing);

        excelsheets = excelappworkbook.Worksheets;

        excelworksheet = (Excel.Worksheet)excelsheets.get_Item(1);
        excelcells = excelworksheet.get_Range("A1", "A1");

        excelcells.Value2 = 10.5;
       }
    }
  }
 }
4
Can you please mention on which line are you getting the exception.Sujay Ghosh
excelcells = excelworksheet.get_Range("A1", "A1");dima
I am facing the exact same problem when I call WorkSheet.UsedRangeRémy Esmery

4 Answers

1
votes

looks like a type library conflict. maybe you have multiple versions installed. please check you have a reference to the type lib you want

9.0 Excel 2000

10.0 Excel 2002

11.0 Excel 2003

12.0 Excel 2007

14.0 Excel 2010

after reference change, clean solution and do a complete recompile.

0
votes

This is not really answering the question directly, but I have seen many people struggle with controlling Excel via the COM interface.

I got a library called FlexCel from TMS Software which lets you create or manipulate Excel worksheets entirely in code, without even needing Excel installed on the machine.

It has lots of other useful features as well (writing PDF files, doing complex reports from Excel templates, etc., etc.)

I should point out that I have no connection with the company, other than being a satisfied user.

0
votes

Can you please try the following and let me know. The following code should work in C# 3.0

excelcells = excelworksheet.get_Range("A1", Type.Missing);

If you are using C# 4.0 , use the following

excelcells = excelworksheet.get_Range("A1");

Also always write code with a try / catch block .

0
votes

Try this: Use Add Remove programs> Go to the tab Change or remove programs Go to Microsoft Office Professional 2003. Click the link "Click here for support information". Use Repair Button.

This worked for me.