首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerPoint.Application.Presentations缺失MsoTriState

PowerPoint.Application.Presentations缺失MsoTriState
EN

Stack Overflow用户
提问于 2022-01-03 07:20:20
回答 1查看 389关注 0票数 4

我试图使用PowerPoint从C#自动处理一个进程,为此,我想打开(或创建一个新的) PowerPoint演示文稿,添加幻灯片,并保存文档。

我已经在我的机器上安装了整个Office2019包,并且可以通过引用Interop.Microsoft.Office.Interop.PowerPoint (来自MicrosoftOffice16.0对象库引用)和Interop.Microsoft.Office.Core (来自MicrosoftOffice16.0对象库引用)来访问ppt。

我尝试使用以下代码打开powerpoint:

代码语言:javascript
运行
复制
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

    }
}

但是,这会导致行pptApplication.Presentations.Open上出现错误)

错误CS0012类型'MsoTriState‘是在未引用的程序集中定义的。必须添加对程序集'office、Version=15.0.0.0、Culture=neutral、PublicKeyToken=71e9bce111e9429c‘的引用

尽管MsoTriState是在Microsoft.Office.Core中定义的,这是office.dll程序集(msdn参考)的一部分。

当我尝试使用to 2019的快速操作时,我可以选择“添加对”OfficeVersion15.0.0.0“的引用”。执行此快速操作将打开references,但不会添加任何引用。手动搜索"Office“也不会产生任何简单命名为"office”的结果。最近的是“MicrosoftOffice16.0对象库”,它已经被引用。

据我所知,这是函数参数所要求的相同的MsoTriStrate,那么为什么它不接受这个呢?试图将MsoTriState值替换为其整数值(例如-1代替true)也不起作用。

使用.NET Core 3.1 WinForms与Office2019(包括。W10 x64 enterprise和VisualStudio2019上安装了Office/Sharepoint开发工具集。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-13 21:07:38

看起来互操作程序集是由tlbimp在某一点上不正确地生成的。您可以在\obj\Debug\netcoreapp3.1\Interop.Microsoft.Office.Interop.PowerPoint.dll找到该程序集。

要正确地重新生成它,您需要执行以下操作:

  1. 删除对"Microsoft PowerPoint 16.0对象库“的引用。清理和重建项目。此时,您还可以尝试卸载项目并手动删除binobj文件夹。
  2. 添加对“Microsoft15.0对象库”和“Microsoft16.0对象库”的引用。
  3. 添加对"Microsoft PowerPoint 16.0对象库“的引用,并再次清理和重建该项目。

在执行这些步骤之后,成功地编译了我的.NET Core3.1 WinForms项目。

下面是我的例子中.csproj文件的内容:

代码语言:javascript
运行
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70562678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档