我试图使用PowerPoint从C#自动处理一个进程,为此,我想打开(或创建一个新的) PowerPoint演示文稿,添加幻灯片,并保存文档。
我已经在我的机器上安装了整个Office2019包,并且可以通过引用Interop.Microsoft.Office.Interop.PowerPoint
(来自MicrosoftOffice16.0对象库引用)和Interop.Microsoft.Office.Core
(来自MicrosoftOffice16.0对象库引用)来访问ppt。
我尝试使用以下代码打开powerpoint:
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开发工具集。
发布于 2022-01-13 21:07:38
看起来互操作程序集是由tlbimp
在某一点上不正确地生成的。您可以在\obj\Debug\netcoreapp3.1\Interop.Microsoft.Office.Interop.PowerPoint.dll
找到该程序集。
要正确地重新生成它,您需要执行以下操作:
bin
和obj
文件夹。在执行这些步骤之后,成功地编译了我的.NET Core3.1 WinForms项目。
下面是我的例子中.csproj
文件的内容:
<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>
https://stackoverflow.com/questions/70562678
复制相似问题