在 Winforms 应用程序中选择 Office 2003/2007 COM 对象(正确的一个)

发布于 2024-07-07 02:30:45 字数 136 浏览 3 评论 0 原文

我们正在创建一个 Windows 窗体应用程序(C# 或 VB.NET),它需要引用 Office 2003 或 Office 2007 COM 对象,具体取决于安装的 Office 版本。 处理这种情况并在运行时引用正确的 COM 对象的最佳方法是什么?

We are creating a Windows Form application (C# or VB.NET) that needs to reference an Office 2003 or Office 2007 COM object, depending on the version of office installed. What is the best way to handle this scenario and reference the correct COM object at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-07-14 02:30:45

除非您想要使用 Office 2007 对象模型的任何新添加的对象和方法,否则可以引用 Office 2003 PIA 进行构建,只需确保在目标系统上部署正确版本的 PIA 即可:

解决此问题的另一种方法是删除对后续 PIA 的依赖。 由于 Office 具有高度的向后兼容性,您可以放心地假设,如果您的外接程序适用于 Office 2003(使用 Office 2003 PIA),那么它也应该适用于 Office 2007(使用 Office 2007 PIA)。< /p>

(来自 无需 PIA 的多个 Office 版本的插件,作者:Andrew Whitechapel)

否则,我会向您推荐 Andrew Whitechapel 的以下博客文章:您能否为多个版本的 Office 构建一个加载项?(请参阅重要警告,表明这不受 Microsoft 官方支持)。

不需要 PIA 的另一种选择(这使得部署变得更加容易)是将ComImport与后期绑定一起使用。 然而,这比使用互操作程序集慢,但如果自动化代码不在快速路径上,这可能是一个很好的解决方案。 您将在同一篇博客文章中找到如何实现此功能的说明:无需 PIA 的多个 Office 版本的加载项

Unless you want to use any of the newly added objects and methods of the Office 2007 object model, it is fine to build referencing the Office 2003 PIAs, just make sure the correct version of the PIAs is deployed on the target system:

Another way around this problem is to remove the dependency on the later PIAs. Because of the high degree of backwards compatibility in Office, you can safely assume that if your add-in works on Office 2003 (with the Office 2003 PIAs), then it should also work on Office 2007 (with the Office 2007 PIAs).

(from Add-ins for Multiple Office Versions without PIAs by Andrew Whitechapel)

Otherwise I would recommend you the following blog articles by Andrew Whitechapel: Can you build one add-in for multiple versions of Office? (See the BIG warning that this is not officially supported by Microsoft).

Another option where you do not need the PIAs (this makes deployment a lot easier) would be to use ComImport together with late binding. This is however slower than using the interop assemblies, but if the automation code is not on the fast path this might be a good solution. You'll find an explanation how to implement this in the same blog post: Add-ins for Multiple Office Versions without PIAs

淡看悲欢离合 2024-07-14 02:30:45

Office 的主互操作程序集对此没有帮助吗? 我不确定,因为我还没有认真使用它们,但我认为它们会的。

Would the Primary Interop assemblies for Office not help with this? I don't know for sure as I haven't had to use them in earnest, but I think they would.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文