Class not registered error when creating Excel wor

2019-03-14 07:08发布

When I try to access an Excel spreadsheet using the following code I get a "Library not registered' error when defining the workbook object wrkbuk using C# from Visual Studio 2012 with Office 2007 (ver 12) installed

Microsoft.Office.Interop.Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application();
        string bookname = @"C:\Users\Public\Documents\RECRUITMENT & SELECTION\MOVEMENTS\MOVEMENTS\Miscellaneous Documents\VacanciesREAL.xls";
        Workbook wrkbuk = excapp.Workbooks.Open(bookname); 
        Worksheet wrksht = new Worksheet();

The error details are

System.InvalidCastException was unhandled HResult=-2147467262
Message=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)). Source=mscorlib

I've created a reference to Microsoft.Office.Interop.Excel.dll in the Office 14 Primary Interop Assembly that comes with VS 2012 and also for the Office 12 version of the dll and neither fixes the problem. I've tried registering the dlls with regasm but this does not help either.

I can create the worksheet wrksht on the following line OK with the Office 14 and Office 12 dlls so the problem seems only affect workbook definition.

Dec 17 2013

Tried reinstalling Office 2007 to no avail but found this solution, which worked. It was at

http://social.msdn.microsoft.com/Forums/vstudio/en-US/d3f92da7-96d3-404b-89d0-d236800ceae5/vs-2012-rc-and-visual-studio-tools-for-office?forum=vsto

Problem is code from multiple versions of Office - I had code from Office 14, possibly coming from VS 2012 install

I suspect that VS 2012 RC has installed the Office 2013 type libraries, and that there are now duplicate versions registered. I got that specific error message

('unable to cast _Application'....'TYPE_E_LIBNOTREGISTERED')

after a messy uninstall, and then re-installing an older version of Office. I solved it by following the advice of another forum thread here which I'm sadly unable to find...basically I searched the registry for the CLSID in the error message, so, {00020970-0000-0000-C000-000000000046}. Its registry key contained two keys, one of which was called 'TypeLib', which in turn contained another CLSID for the type library. I then searched the registry, starting at the beginning again, for that second CLSID, which led me to the relevant interop type library. It had two entries....8.3 and 8.4, the bigger number corresponding to the later version of Office....which I deleted...and immediately was able to run my program.

7条回答
萌系小妹纸
2楼-- · 2019-03-14 07:30

I had exactly the same issue. If you have installed a new version of Office and for some reason you have to install the older version without removing the new one (For my case I just need to install the Office XP Photo Editor) and this caused the issue.

After spending 3 hours up and down, the best way is to repair the new version of Office and everything was back to normal as it should be.

Hope this info helps provided you have the same or similar scenario.

查看更多
小情绪 Triste *
3楼-- · 2019-03-14 07:36

I had this exact same problem. Here's how I fixed it:

Go to

HKEY_CLASSES_ROOT\TypeLib\ and search for Office.Interop.Excel.

I found HKEY_CLASSES_ROOT\TypeLib\(guid)\1.8 but its child nodes were empty! I deleted it because Office 15 wasn't installed on that machine, and hey presto - it worked.

Either our VSTO project installer, or our own custom installer must have placed the 1.8 folder there.

I found the solution here:

Error accessing COM components

查看更多
欢心
4楼-- · 2019-03-14 07:37

I will confirm @stuzor 's answer on two machines I've encountered.

Background:

Windows7 64 bit
Office 2010 (32bit) 
Lync 2013 

HKEY_CLASSES_ROOT\TypeLib\ , searched for "Office.Interop.Excel". Found the barely existent "1.8" folder. Upon deletion, the function in an old VB.net app began working immediately.

查看更多
可以哭但决不认输i
5楼-- · 2019-03-14 07:39

The registry hack described above fixed the same problem for me. The only difference was I had a 1.9 and a 1.7 entry. I deleted the 1.9 key and my problem was fixed.

Other info:

OS: Windows 10

Visual Studio Professional 2015

C# Windows Form Application

This was on my work laptop. We have Office 2010 installed on each workstation.

Some other things I tried that DID NOT WORK were the following:

Repairing MS OFFICE

Reinstalling MS OFFICE

Downloading Microsoft Office 2010: Primary Interop Assemblies from Microsoft.

I also had several versions of the Microsoft.Office.Interop.Excel.dll on my laptop. Deleting previous versions did not fix the problem.

Glad I went to STACKOVERFLOW.COM because this place ROCKS!!!!!!

查看更多
够拽才男人
6楼-- · 2019-03-14 07:40

We had the same problem.

Our setup is like this :
Office 2010 : Word and Excel but Office 2013 : Outlook

We ran the installation program for Office 2010 and choose the repair option. After that, all the wrong keys in the registry for Word and Excel were deleted and the problem was solved.

查看更多
你好瞎i
7楼-- · 2019-03-14 07:46

Something which hasn't been mentioned here but which may be useful is that subkeys of 1.XX can cause issues as well. For example: Under Computer\HKEY_CLASSES_ROOT\TypeLib{00020813-0000-0000-C000-000000000046} I had the subkey 1.9. This in turn has the subkey 0(or should). Now under this, there were two subkeys - Win32 and Win64. As a result, Visual Studio was unable to resolve the location of Microsoft.Office.Interop.Excel.dll - it was reading two subkeys where it could only read one. Deleting the Win32 subkey fixed my issue completely. Please note that this was done after a complete reinstall of VS 2017 & Office 365 Pro Plus. Hope this helps someone.

查看更多
登录 后发表回答