Search code examples
c#office-interop

understanding COM exception


Hey I'm getting the following exception: Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154. Office 2010.

I've read a bunch of results and the typical solution is change debug from AnyCPU to X86.(I don't want to do this because it's a large project, but it didn't work anyway). I want to understand the exception as well.

I was simply attempting to add a new workbook worksheet and fill it with some data, but it errs out on the creation of the Workbook due to the above exception.

using excel = Microsoft.Office.Interop.Excel;

public static void ExcelFunction()
    {
        excel.Workbook wb_XLS = new excel.Workbook();
        excel.Worksheet ws_XLS = new excel.Worksheet();
        ws_XLS = (excel.Worksheet)wb_XLS.ActiveSheet;
        int x, y,count;
        count = x = y = 0;
        while (x < 100)
        {
            while (y < 100)
            {
                ws_XLS.Cells[x, y] = count.ToString();
                count++;
                y++;
            }
            x++;
        }
    }

Solution

  • Shouldn't you first create excel application and then create a new workbook by calling Add on workbooks collection rather than calling new on excel.workbook?

     Excel.Application app = new ... ;
     var workbook = app.Workbooks.Add();