Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT: 0x80040155
Asked Answered
M

3

8

I'm having an issue with a very simple piece of C# .NET code which should (for now) simply retrieve the version number of Office installed on the computer running a WinForms application:

var oApp = new Outlook.ApplicationClass();
var outlookVersionString = oApp.Version;

It instantiates the object correct, however when I try and access the Version property, I get the following:

{"Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155)."}

I've done a lot of searching online, so a run-through of what I've tried:

  1. Checking that in [HKEY_CLASSES_ROOT\TypeLib\{00062FFF-0000-0000-C000-000000000046}] only one Key exists (9.6)
  2. Checking that this ClassID and version matches in [HKEY_CLASSES_ROOT\Interface\{00063001-0000-0000-C000-000000000046}]
  3. Registering MSOUTL.OLB with C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12.exe "C:\Program Files (x86)\Microsoft Office\root\Office16\MSOUTL.OLB" (fails because regtlibv12.exe isn't shipped with VS2017)
  4. Downloading regtlibv12.exe and trying again (fails because 'This app can't run on your PC').
  5. Running C:\Users\uczms>c:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /TLB "C:\Program Files (x86)\Microsoft Office\root\Office16\MSOUTL.OLB" (fails because it is not a valid .NET Assembly)
  6. Running C:\Users\uczms>c:\Windows\Microsoft.NET\Framework64\v2.0.50727\regasm.exe /TLB "C:\Program Files (x86)\Microsoft Office\root\Office16\MSOUTL.OLB" (fails because it is not a valid .NET Assembly)
  7. Using what worked with the old Interop libraries (edit: note, this does not appear to work with our Office 2016 desktops), which is a subtle change:

var oApp = new Outlook.Application(); var outlookVersionString = oApp.Version;

It might be worth noting that I have a clean Windows 10 Enterprise N Creators Update, and Office 2016 / 365 ProPlus, haven't had any previous versions installed, and haven't downgraded.

I'm not normally a desktop app developer so while I grasp some of these library registration tools, I'm by no means an expert..

Micropaleontology answered 16/11, 2017 at 14:34 Comment(12)
you're using the wrong item, try: var oApp = new Outlook.Application();Mazer
@Rob, See point 7 above (editing now to add code formatting)Micropaleontology
@MattStyles Which is your actual question? You seem to have a working solution with Point 7? Or is the question why your original code does not work?Pierro
@Pierro the code in point 7 works with the old Interop libraries, which only work until some older version of Office, but our desktops are all being upgraded to Office 2016. The code has never worked with Office 2016.Micropaleontology
@Pierro correct. In many ways I wish I didn't have to touch this application because all of our apps' integration with Office just seem so faffy and any upgrade work results in a new problem.Micropaleontology
Ah, now I see. That wasn't clear from the question.Pierro
I'm on 2016, and using Outlook.Application() works for me.Mazer
@Rob, cool. The problem is.. it doesn't work for me :)Micropaleontology
Curious, is the dev machine on 2016? Tried removing the reference and adding it back again (to the latest) - it may be trying to hang on to the old ref.Mazer
@HansPassant the Main() entrypoint is already decorated with [STAThread]. Selecting different build CPU targets doesn't appear to make a difference.Micropaleontology
@Rob, interesting - I removed the reference (which was to 14.0), went to re-add it, it added 12.0, and it works. Curious as to how I got a reference to 14.0 in the first place.. Also the Word Interop 14.0 works fine..Micropaleontology
Sigh, who knows why these things happen. Just take the win and don't worry about it any more :-)Mazer
A
7

Try to "repair" your Microsoft Office installation. Go to Programs and Features, select Modify and then Repair Online. I had the same issue and this fixed it.

Adria answered 24/5, 2018 at 19:55 Comment(2)
It is a little different on Windows 10. Use "Apps and Features" and then find your Office installation and then choose Modify. The 'Quick Repair' worked for my situation. I was getting the following: System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._ApplicationCorrigible
In my case, the error was caused by installing a 32bit program that uses the Access Database Engine. I had to uninstall both of the installations listed before I could reinstall Office. That took care of the error.Stull
H
3

I appreciate this is an old topic, my issue was Visual Studio defaulting to 'Prefer 32-bit' in the project's compile properties for .NET Framework 4.5+. !

Screenshot of Visual Studio Prefer 32-bit checkbox:
Screenshot of Visual Studio Prefer 32-bit checkbox

How answered 28/8, 2022 at 17:15 Comment(0)
D
2

Some my customers had a similar issues on some PC and the only action that have solved was usign the point 3 "Ensure MSOUTL.LIB is Registered" of this fantastic guide webmakers - Interface not registered (Exception from HRESULT: 0x80040155) – Microsoft Office/Outlook Error, after checked the previous point, obviously. Thanks to Jatin-Auckland for a clear and step by step guide.

Goodluck ;)

Dysarthria answered 24/5, 2022 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.