다음을 통해 공유


C# Troubleshooting: Unable to Cast COM Object of Type 'Microsoft.Office.Interop.PowerPoint. ApplicationClass'

My team has been struggling with COM Component issue for about one week, it almost frustrated the developer, me and project manager. Uff.. after an intense amount of googling and trial method, several time repair and installation of office, the problem was still bugging all the team members. Finally, the issue was resolved with a couple of settings in Registry. The real cause of the issue was a mystery. Finally, we succeeded to identify the reason and then killed the exception which was constantly thrown by the system.

Exception is thrown by the system:

Below is the most boring exception frustrated me every time when I debug the code

System.InvalidCastException was unhandled

Message=Unable to cast COM object of type 'Microsoft.Office.Interop.PowerPoint.ApplicationClass' to interface type 'Microsoft.Office.Interop.PowerPoint._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{91493442-5A91-11CF-8700-00AA0060263B}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

Source=Microsoft.Office.Interop.PowerPoint

Problem Description:

The simple requirement was to convert office document to pdf using c# code. For other office format doc/docs, xls/xlsx the code was working perfectly fine. Only the problem was coming with ppt/pptx file.

The actual problem was in code when I am trying to open the ppt file, using the Interop application class. Below is the sample code where the exception keeps on throwing every time.

Solution:

Let me explain you the cause, my developer has done several times installation and uninstalled office 2010 and office 2013. What we wanted is to run the code with Office 2010, so Office 2013 was out from the system. Now with this install and uninstall of Office 2013 what happened that the registry key for the TypeLib was still there in the system registry. And when the c# code tries to open the ppt file it was trying to load the TypeLib of Office 2013 which was not present in the system file but was there in the registry.

In your scenario, you might get the same exception but the reason could be different. You might try the steps which explained below to resolve your issue. In my case, it works perfectly fine.

Step1:

Copy the CLSID from the exception as shown above. In my case it was {91493442-5A91-11CF-8700-00AA0060263B}

Step2:

Now got to Start->Run->regedit it will open the registry window.

Step3:

Now click on HKEY_CLASSES_ROOT and press ctrl+F to search for the CLSID which you copied in step1

Step4:

You will get the CLSID {91493442-5A91-11CF-8700-00AA0060263B}

 Inside Interface under the HKEY_CLASS_ROOT. Explore this CLSID you will see a sub-root TypeLib. Click on TypeLib and in the right side, you will see default key entry which will be another CLSID. You can copy this CLSID now for the next step

Step5:

Now press T on your keyboard and navigate to the TypeLib folder under the root HKEY_CLASSES_ROOT

Click on the TypeLib folder and then press Ctrl+F to find the other CLSID key which you copied in step 4

Step6:

Once you find the CLSID, expand it you will see all couple of folder categorized with version, in my case I found 2.a, 2.b as shown in below screenshot

Now what you need to do is expand each version of the folder till and go to win32. It will display a file path as shown above. Now you copy the file path and see if the file shown at this path exist in your system. If the file does not exist you can simply delete that folder.

In my case, I found that folder 2.b->0->win32 was referring a file path that does not exist on my system, so I deleted it and the problem was resolved.