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

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

问题描述

我不得不将一个较旧的项目(在.Net 2中)导入Visual Studio 2013,它利用了Microsoft Primary Interop程序集.

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说我需要添加对该项目的引用.现在,我去阅读了一些书,显然微软只发布了Office 2010的PIA吗? (我有Office 2013)

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)

现在我想知道的是.

  1. 我可以使其与Office 2013一起使用并向后兼容吗?
  2. 如果是的话,这是通往未来的好途径吗?它会兼容吗?因为我看到您需要.Net 2(最晚),并且Windows 8带有4.5,而不是3(默认情况下),并且大多数新计算机都将装有Office 2012或2013.
  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.

推荐答案

PIA是历史工件,只有旧的.NET版本(在v4之前)才需要.它们已被嵌入互操作类型"功能(也称为无PIA"功能)彻底而优雅地替换.自Visual Studio 2010以来受支持,选择参考部件后,您将在属性"窗口中找到它.默认为True.涵盖基础技术的优秀视频是在此处可用.

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.

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

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

此功能非常理想,它可以避免客户在其计算机上安装PIA并避免将其包含在安装程序中.如果两个人都不去解决问题,那就太普遍了.此外,Office的PIA很大,嵌入互操作类型的最大好处是程序集仅包含您实际使用的类型.许多兆字节减少到几千字节.

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.

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

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.

请注意,某个功能丢失了,而有意针对实际上尚未在开发机器上安装的Office的旧版本,则更加困难.如果这是必需的,那么您仍需要该版本的PIA,请在属性"窗口中将嵌入互操作类型"强制为True.实际上这样做是有问题的,Microsoft很难使新的Office版本与旧版本完全兼容.他们已经将它保存了15年,但是它已经耗尽了精力.最坏的情况是针对比您计算机上已安装的版本更高的版本,这很容易使程序崩溃,并且非常很难诊断像AccessViolationException这样的异常.

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.

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

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.

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

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