gpt4 book ai didi

c# - 嵌入互操作类型问题

转载 作者:行者123 更新时间:2023-11-30 19:57:14 28 4
gpt4 key购买 nike

当然 Embed interop types 功能是个好东西,但即使在简单的场景中我也无法使用它,所以请指教这是我在没有安装 powerPoint 的机器上运行我的项目时遇到的错误:

enter image description here

我的代码非常简单,我只是从 powerPoint 创建对象,创建演示文稿并在其中滑动写入一些东西。

我嵌入的库是 OfficeMicrosoft.Office.Interop.PowerPoint

将构建配置转换为 x68 并没有解决问题,

我正在构建 Windows 应用程序并将代码放入按钮点击中,如下所示:

 private void button1_Click(object sender, EventArgs e)
{
var pp = new powerpoint.Application();
var oPres=pp.Presentations;
pp.Visible = Office.MsoTriState.msoTrue;
powerpoint.Presentation oPre= oPres.Add(Office.MsoTriState.msoTrue);

powerpoint.Slides oSlides = oPre.Slides;
powerpoint.Slide oSlide = oSlides.Add(1, powerpoint.PpSlideLayout.ppLayoutText);
powerpoint.Shapes oShapes = oSlide.Shapes;
powerpoint.Shape oShape = oShapes[1];
powerpoint.TextFrame oTxtFrame = oShape.TextFrame;
powerpoint.TextRange oTxtRange = oTxtFrame.TextRange;
oTxtRange.Text = "All-In-One Code Framework";

string fileName = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\Sample1.pptx";
oPre.SaveAs(fileName,
powerpoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
Office.MsoTriState.msoTriStateMixed);
oPre.Close();

pp.Quit();
pp = null;
}

在我添加的顶部

 using powerpoint = Microsoft.Office.Interop.PowerPoint; 
using Office = Microsoft.Office.Core;
using System.IO;
using System.Reflection;

注意:该程序在我安装了 Office 2013 的那一端运行良好,但此错误出现在我的客户端电脑上

最佳答案

如前所述,问题在于客户端计算机上未安装 Powerpoint。

powerpoint对象在COM类型库中实现,在安装Powerpoint时安装注册。 COM 和.NET 是完全不同的技术。要在 .NET 应用程序中使用 COM 类型,您不直接使用 COM 类型,而是使用特殊的互操作 .NET 程序集。该程序集不包含任何 PPT 功能,它只是一个包装器,充当 .NET 应用程序和 COM 类型之间的桥梁。互操作程序集会为您完成所有艰苦的工作,并定义您可以用作其他 .NET 类的 .NET 类型(来自 COM 类型),例如 powerpoint.Application

互操作程序集只是一个普通的 .NET 程序集。您可以将其作为其他 .NET 引用来引用(Embed Interop Types = false)。在这种情况下,您需要将互操作 DLL 与您的应用程序一起分发。如果您设置 Embed Interop Types = true,则互操作程序集将被编译并直接嵌入到您的应用程序程序集中。而且,只嵌入真正使用的类型和函数。因此使用此选项具有优化和单一装配的优势。

但是,即使嵌入,互操作信息也只是对必须安装在客户端计算机上的真实 COM 类型的包装。如果不是,您将收到错误消息。查看更多详情 https://msdn.microsoft.com/en-us/library/xwzy44e4.aspx

您的选择是强制客户端安装 PPT 或避免使用 Microsoft.Office.Interop.PowerPoint 并使用您可以随应用程序分发的一些第三方 PPT 库。

关于c# - 嵌入互操作类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964794/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com