3
votes
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.

1
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

1 Answers

1
votes

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