Search code examples
c#referencepowerpoint

PowerPoint.Application.Presentations missing MsoTriState


I'm trying to automate a process using PowerPoint from C#, to do this I want to open (or create a new) PowerPoint presentation, add a slide, and save the document.

I've got the entire office 2019 package installed on my machine and can access the ppt api by referencing the Interop.Microsoft.Office.Interop.PowerPoint (from the Microsoft PowerPoint 16.0 Object Library reference) along with Interop.Microsoft.Office.Core (from the Microsoft Office 16.0 Object Library reference).

I try to open a powerpoint using the following code:

using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

class PowerPointManager
{
    public PowerPointManager()
    {
        string powerPointFileName = @"C:\temp\test.pptx";

        Application pptApplication = new Application();
        Presentation ppt = pptApplication.Presentations.Open(powerPointFileName, MsoTriState.msoTrue); //MsoTriState comes from Microsoft.Office.Core

    }
}

However this results in an error on the line pptApplication.Presentations.Open)

Error CS0012 The type 'MsoTriState' is defined in an assembly that is not referenced. You must add a reference to assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

Even though MsoTriState is most definitely defined in Microsoft.Office.Core which is part of the office.dll assembly (msdn reference)

When I try to use VS2019's quick actions I get the option to "Add reference to "office Version 15.0.0.0"". Performing this quick actions opens up the Reference Manager, but does not add any references. Manually searching for "Office" doesn't yield any result simply named "office" either. The closest being "Microsoft Office 16.0 Object Library", which is already referenced.

As far as I can tell this is the same MsoTriStrate the function parameter is asking for, so why is it not accepting this? Trying to substitute the MsoTriState value for its integer value (e.g. -1 for true) doesn't work either.

Using .NET Core 3.1 WinForms, with Office 2019 (incl. powerpoint) 32bit, on W10 x64 enterprise and Visual Studio 2019 with the Office/Sharepoint development toolset installed.


Solution

  • It looks like the interop assemblies were generated improperly by tlbimp at the certain point. You can find that assembly at \obj\Debug\netcoreapp3.1\Interop.Microsoft.Office.Interop.PowerPoint.dll.

    To re-generate it properly, you need to do the following:

    1. Remove the reference to "Microsoft PowerPoint 16.0 Object Library". Clean and rebuild the project. You may also try unloading the project and removing the bin and obj folders manually at this point.
    2. Add references to both "Microsoft 15.0 Object Library" and "Microsoft 16.0 Object Library".
    3. Add back reference to "Microsoft PowerPoint 16.0 Object Library", and clean and rebuild the project once more.

    After performing these steps, my .NET Core 3.1 WinForms project was compiled successfully.

    Here are the contents of the .csproj file in my case:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
      </PropertyGroup>
    
      <ItemGroup>
        <COMReference Include="Microsoft.Office.Core">
          <WrapperTool>tlbimp</WrapperTool>
          <VersionMinor>8</VersionMinor>
          <VersionMajor>2</VersionMajor>
          <Guid>2df8d04c-5bfa-101b-bde5-00aa0044de52</Guid>
          <Lcid>0</Lcid>
          <Isolated>false</Isolated>
          <EmbedInteropTypes>true</EmbedInteropTypes>
        </COMReference>
        <COMReference Include="Microsoft.Office.Interop.PowerPoint">
          <WrapperTool>tlbimp</WrapperTool>
          <VersionMinor>12</VersionMinor>
          <VersionMajor>2</VersionMajor>
          <Guid>91493440-5a91-11cf-8700-00aa0060263b</Guid>
          <Lcid>0</Lcid>
          <Isolated>false</Isolated>
          <EmbedInteropTypes>true</EmbedInteropTypes>
        </COMReference>
      </ItemGroup>
    
    </Project>