Search code examples
c#outlookoffice-interopcom-interop.net-6.0

Microsoft.Office.Interop.Outlook error in .net core


I have a program that accesses Outlook using the COM-object Microsoft.Office.Interop.Outlook.

The first part of the program creates a new Outlook Application and fetches the allpublicfolders folder.

using Outlook = Microsoft.Office.Interop.Outlook;
Outlook.Application application = new Outlook.Application();
Outlook.Folder allPublicFolder = 
    application
    .Session
    .GetDefaultFolder (Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders) 
     as Outlook.Folder;

The program was originally targeting .NET Framework and was working as intended. But after I recently moved it to .NET 6 I get an red error line underneath the .Session part.

The error message states the following:

"Error CS1061 'Application' does not contain a definition for 'Session' and no accessible extension method 'Session' accepting a first argument of type 'Application' could be found (are you missing a using directive or an assembly reference?)"

Any ideas as to what could be causing this problem?

EDIT: After removing the second line it turns out that even the first line (new Outlook.Application()) returns an error: "Unable to cast COM object of type 'System_ComObject' to interface type 'Microsoft.Office.Interloop.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: Element not found. (0x8002802B (TYPE_E_ELEMENTNOTFOUND))"


Solution

  • The error was caused by missing the component [HKEY_CLASSES_ROOT\Interface{00063001-0000-0000-C000-000000000046}] in the Registry Editor. Repairing office didn't help, but reinstalling office created the component and solved the error.