Search code examples
c#.netoutlookoffice-interopcom-automation

I can't trigger Microsoft.Office.Interop.Outlook.dll via .NET


I have installed Visual Studio Tools for Office (VSTO) so can use Microsoft.Office.Interop.Outlook.dll for my .Net applications. However, I get the error below. I think it is related to registry keys but I am not sure. Microsoft.Office.Interop.Outlook.dll exists but the registry is looking for it somewhere else maybe? I used to use this .dll library on my previous computer w/o problem. By then, I did not install VSTO but it was something similar. Do you have any idea how can I fix the issue?

3rd line raises the exception below on the screenshot

using Microsoft.Office.Interop.Outlook;

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

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

https://social.msdn.microsoft.com/Forums/vstudio/en-US/ac50fa41-8d47-4fa9-81a3-914f262676af/0x80029c4a-typeecantloadlibrary?forum=vsto

but they didn't help

enter image description here

enter image description here


Solution

  • I backed up and deleted both Win64 Keys under

    00062FFF-… 2DF8D04C…

    Then it worked.

    Here is my guess:

    We upgraded the latest 2021 Office (64 bit) and downgraded back to 2016 Office (32 bit) and I believe this was the root of the problem.

    I think VS was trying to use 64-bit Outlook but of course it was not there. So, it was showing broken references (yellow triangle sign) on references of

    Microsoft.Office.Core Microsoft.Office.Interop.Outlook

    I tried to browse and connect the library (i.e., right-click References > Add Reference > Browse) directly but had no luck. I think VS was still referring to 64-bit Office/Outlook

    When I removed the Win64 keys VS was forced to use 32-bit Office which exists and it worked fine.

    It took a while to figure this out I hope this answer helps other users. By the way, I don't know if deleting these registries will cause any problem when we upgrade later Office version again.