Error loading type library/DLL (Microsoft.Office.Interop.Excel.Application)

I was struggling with this error for a few days . It came out after I’ve upgraded my project to Visual Studio 2012.

Unable to cast COM object of type ‘System.__ComObject’ 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: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

It shows up while it’s the time for Excel operations:

WeDem

Workaround:

It occurs because I’ve installed Office 2013 instead of Office 2010 which I was using with old version of my project. After I upgraded my Office, the project was no longer able to link Excel reference.

Even though I referenced new Excel library which is 15.0, it didn’t work.

Solution:

  • Delete the Excel reference (Microsoft.Office.Interop.Excel). Version 15.0, in my case.
  • Install Microsoft Excel (whichever version you had been using. Version 2010 for me)
  • Add the Excel reference (older one. Version 14.0, in my case)

Long story short: Never take more than one risk at the same time!

DisconnectedContext error

Solved: “DisconnectedContext was detected” error when releasing Excel object in C#

You may face “DisconnectedContext was detected” error if you work with Excel interop objects in C#. This error causes crashing your application. It is really annoying issue and there is no specific resolution for this. So, I’ll give the general idea in this post.

Context 0xdf76a0′ is disconnected. Releasing the interfaces from the current context (context 0xdf7450). This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

DisconnectedContext error
DisconnectedContext error

Your code may look like this:

Excel._Application app = new Excel.Application();
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
app.Visible = true;

worksheet.Cells[1, 1] = "test string";

workbook.SaveAs("C:\testfile.xlsx");

object misValue = System.Reflection.Missing.Value;
workbook.Close(true, misValue, misValue);
app.Quit();

releaseObject(worksheet);
releaseObject(workbook);
releaseObject(app);

At the first time the debugger goes into releaseObject, it gives DisconnectedContext error at first GC.WaitForPendingFinalizers() line in my case.

Solution

This issue has a quite complicated background. In order to simplify it, I wrote a post that is related to this problem a few days ago. Check this out:

http://port135.com/2012/08/08/how-to-properly-clean-up-excel-interop-objects-in-c/

If it doesn’t help:

  1. Make sure you release all objects accurately. Study Runtime Callable Wrapper and ReleaseComObject.
  2. Try to run your application in “release mode” instead of “debug mode“. Sometime this error doesn’t show up in “release mode”. So, you may want not to do anything about it.
  3. If the problem is still going on, publish your application (Project > Properties > Publish). Do you have the same problem? If you don’t, it doesn’t mean you have resolved it. You have just ignored it. If you want to resolve it forever, go back to step 1 and try hard.