Mads
Mads

Reputation: 27

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))"

Upvotes: 0

Views: 3006

Answers (3)

James
James

Reputation: 1

I had this same problem and found that I had a duplicate installation of 365 on my machine. Uninstalled one of the installations and it solved this problem.

Upvotes: 0

Mads
Mads

Reputation: 27

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.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49445

I have just tried to create a console app based on .net 6 in VS2022 and don't get any problems with compiling and running the application under the debugger. Everything went well on my side. Here is my project's file content:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>ConsoleApp3</RootNamespace>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Outlook">
      <WrapperTool>tlbimp</WrapperTool>
      <VersionMinor>6</VersionMinor>
      <VersionMajor>9</VersionMajor>
      <Guid>00062fff-0000-0000-c000-000000000046</Guid>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
      <EmbedInteropTypes>true</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>

</Project>

Make sure the target platform was set to AnyCPU.

Upvotes: 2

Related Questions