4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace GridComplete
{
    public partial class Form1 : Form
    {
        private static Excel.Workbook MyBook;
        private static Excel.Application MyApp;
        private static Excel.Worksheet MySheet;

        private void bRun_Click(object sender, EventArgs e)
        {
            MyApp = new Excel.Application();
            MyApp.Visible = false;
            MyBook = MyApp.Workbooks.Open(tbFile1);
            MySheet = (Excel.Worksheet)MyBook.Sheets[1];

        }

    }
}

On the line that sets MyApp.Visible to false, I get the following unhandled exception:

An unhandled exception of type 'System.InvalidCastException' occurred in GridComplete.exe

Additional information: 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)).

** I can't seem to figure what's going on. I tried to delete keys from the registry and rerun. That didn't work. I also fully uninstalled Microsoft Office, then reinstalled. I'm not sure what else to do to fix this issue.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Marian Montagnino
  • 125
  • 1
  • 4
  • 11
  • The registry on your machine is missing the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Classes\TypeLib\{00020813-0000-0000-C000-000000000046} key. Reinstalling Office certainly ought to fix that, very unhealthy of course. Get another one. – Hans Passant Aug 22 '14 at 00:46

2 Answers2

2

It's possible that you don't have specified version of Microsoft Excel installed on computer where you running that code. Or it's installation broken. Try to re-install Office

rufanov
  • 3,266
  • 1
  • 23
  • 41
1

The problem is caused by the incorrect vesion information in registry. I once installed the Office 365, and later I uninstall it, and install the Offic 2010. So there are two items there,but only one can work.

Firstly you can open registry editor, please must do the backup by File -> Export -> Check Export range to All.

Secondly, you can check the item {000208D5-0000-0000-C000-000000000046} by Ctrl + F, select it and get the value of TypeLib, remember the value. To my computer, it is 1.7, I guess it is for Office 2010, the current installed Office.

Then into HKEY_CLASSES_ROOT\TypeLib, and select item {00020813-0000-0000-C000-000000000046} and extend, I have two subitem, one is 1.7, the other is an empty 1.9. After I delete 1.9, the program works.

Related information in Chinese

Bingoabs
  • 539
  • 5
  • 11