我仍然可以将 Microsoft.Office.Interop 程序集与 office 2013 一起使用吗? [英] Can I still use Microsoft.Office.Interop assemblies with office 2013?

查看:15
本文介绍了我仍然可以将 Microsoft.Office.Interop 程序集与 office 2013 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不将一个旧项目(在 .Net 2 中)导入到 Visual Studio 2013 中,它使用了 Microsoft Primary Interop Assemblies.

Visual Studio 说我需要添加对项目的引用.现在我去读了一些书,显然微软只发布了 Office 2010 的 PIA?(我有 Office 2013)

现在我想知道的是.

  1. 我可以让它与 office 2013 一起使用并向后兼容吗?
  2. 如果是这样,这是通往未来的好途径吗?它会兼容吗?因为我认为您需要 .Net 2(最迟)并且 Windows 8 带有 4.5 而不是 3(默认情况下),并且大多数新计算机将配备 Office 2012 或 2013.

解决方案

PIA 是一个历史产物,只有旧的 .NET 版本(v4 之前)才需要.它们已被嵌入互操作类型"彻底而优雅地取代.功能,也称为无 PIA"特征.自 Visual Studio 2010 起受支持,当您选择引用程序集时,您会在属性"窗口中找到它.它默认为 True.一个涵盖底层技术的好视频是 此处可用.

这就是 Microsoft 不发布 Office 2013 的 PIA 的原因,他们希望您改为嵌入互操作类型.

该功能非常受欢迎,它避免了您的客户必须在他的机器上安装 PIA 以及您将它们包含在您的安装程序中.解决问题时谁都不关心它,这是一个完全太常见的事故.此外,Office 的 PIA 非常大,嵌入互操作类型的巨大优势是您的程序集仅包含您实际使用的类型.从几兆字节减少到几千字节.

工作流程略有不同.您现在可以使用 COM 选项卡,而不是在添加引用"对话框的.NET Framework"选项卡中添加对 Microsoft.Office.Interop 程序集的引用.然后选择Microsoft Excel 15.0 Object Library"为使用 Excel 的程序生成互操作类型.如果您加载以前使用 PIA 的旧项目,则只需删除这些引用程序集,然后从 COM 选项卡中重新添加它们即可.

请注意,某项功能丢失了,故意针对您的开发机器上实际上并未安装的旧版 Office 更加困难.如果这是一个要求,那么您仍然需要该版本的 PIA,在属性"窗口中将嵌入互操作类型"强制为真".实际上这样做是有问题的,微软很难让新的 Office 版本与旧版本完全兼容.他们已经坚持了 15 年,但它已经失去了动力.最坏的情况是针对比您在机器上安装的版本更新的版本,这很可能使您的程序崩溃,并出现非常难以诊断的异常,例如 AccessViolationException.

请注意,您必须对代码进行小的修改才能使其正常工作.合成的XxxxClass"类没有嵌入,只有Xxxx";接口.只需删除单词Class"即可.来自 new 语句.

I had to import an older project (in .Net 2) into Visual Studio 2013, it makes use of the Microsoft Primary Interop Assemblies.

Visual Studio said that I need to add references to the project. Now I went and did some reading and apparently Microsoft has only released the PIA for office 2010? (I have Office 2013)

Now what I would like to know is.

  1. Can I get it to work with office 2013 and be backward compatible?
  2. And if so is this a good route to go for the future? Is it going to be compatible? Because I see you need .Net 2 (at the latest) and Windows 8 comes with 4.5 and not 3 (by default) and most new computers are going to have Office 2012 or 2013.

解决方案

PIAs are a historical artifact, required only by old .NET versions (before v4). They have been thoroughly and elegantly replaced by the "Embed Interop Types" feature, also known as the "No PIA" feature. Supported since Visual Studio 2010, you'll find it back in the Properties window when you select a reference assembly. It defaults to True. A good video that covers the underlying technology is available here.

Which is the reason that Microsoft doesn't publish the PIAs for Office 2013, they expect you to embed the interop types instead.

The feature is very desirable, it avoids your customer having to install the PIAs on his machine and for you to include them with your installer. Solving the issue when neither takes care of it, an entirely too common mishap. In addition, the PIAs for Office are very large, the great advantage of embedding the interop types is that your assembly only contain the types that you actually use. Many megabytes reduced to a few kilobytes.

The workflow is a little different. Instead of adding a reference to the Microsoft.Office.Interop assemblies as available in the Add Reference dialog, .NET Framework tab, you now use the COM tab. And pick, say, "Microsoft Excel 15.0 Object Library" to generate the interop types for a program that uses Excel. If you load an old project that previously used PIAs then just remove those reference assemblies and add them back from the COM tab.

Do note that a feature is lost, intentionally targeting an old version of Office that you don't actually have installed on your dev machine is more difficult. If that's a requirement then you still need the PIAs for that version, force the Embed Interop Types to True in the Properties window. Actually doing this is questionable, Microsoft has a hard time keeping new Office versions completely compatible with old versions. They've kept it up for 15 years now but it has been running out of steam. A worst-case scenario is targeting a newer version than you have installed on your machine, that's liable to make your program crash with very hard to diagnose exceptions like AccessViolationException.

Do note that you have to make small modifications to your code to allow it to work. The synthetic "XxxxClass" classes are not embedded, just the "Xxxx" interfaces. Simply remove the word "Class" from the new statement.

这篇关于我仍然可以将 Microsoft.Office.Interop 程序集与 office 2013 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆