如何从 Visual Studio C# 使用 Office?

发布于 2024-07-11 04:23:26 字数 4823 浏览 8 评论 0原文

在 Visual Studio 中添加对 Office 的 COM 互操作的引用的技术是转到:

  1. 引用
  2. 添加引用
  3. 选择 COM 选项卡
  4. 选择 Microsoft Office 11.0 对象库

并神奇地命名出现引用:

Microsoft.Office.Core

Project.csproj 文件显示了引用的详细信息:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

并且该项目已签入源代码管理,一切正常。


然后,使用 Office 2007 的开发人员从源代码​​管理中获取该项目,但无法构建它,因为这样的引用不存在。

他(即我)检查了 .csproj 文件,删除了对 COM 引用的引用

Microsoft Office 11.0 Object Library

,并将 COM 引用重新添加为

Microsoft Office 12.0 Object Library

并且神奇地出现了一个命名引用:

Microsoft.Office.Core

Project.csproj 文件显示了引用的详细信息:

<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>4</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>

该项目已检查到源代码控制,一切顺利。


然后,使用 Office 2003 的开发人员从源代码​​管理中获取该项目,但无法构建它,因为这样的引用不存在。

他(即不是我)检查了 .csproj 文件,删除了引用

Microsoft Office 12.0 Object Library

并将 COM 引用重新添加为

Microsoft Office 11.0 Object Library

神奇地出现了一个命名引用:

Microsoft.Office.Core

Project.csproj 文件显示了引用的详细信息:

<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>3</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>

该项目已检查到源代码控制,一切都很好。

然后构建项目,压制成CD,并发送给拥有Office 2007 的客户。

一切都不太好。


在过去(即在 .NET dll 地狱之前),我们会使用版本无关的 ProgID 引用 Office 对象,即:

"Excel.Application"

解析为已安装 Office 的 clsid ,例如,

{00024500-0000-0000-C000-000000000046}    

然后使用对 COM(语言无关伪代码)的调用来构造一个类:

public IUnknown CreateOleObject(string className)
{
    IUnknown unk;

    Clsid classID = ProgIDToClassID(className);
    CoCreateInstance(classID, null, 
          CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
          IUnknown, out unk);

    return unk;
}

问题

1) 什么是已批准的技术来自动安装 Office 应用程序?

2) 什么是 Office 2003主互操作程序集有什么用?

3) 如果我使用 Office 2003 主互操作程序集,是否必须安装 Office 2003?

4) 如果我使用 Office 2003 主互操作程序集进行构建,我的客户是否会永远与 Office 20003 绑定在一起?

5) 是否有Office 2007 主要互操作程序集

6) 如果我安装 Office 2007 主互操作程序集,是否必须安装 Office 2007?

7) 使用标准 COM 互操作来驱动 Excel、Word 或 Outlook 有什么问题? 例如:

[ComImport]
[Guid("00024500-0000-0000-C000-000000000046")]
public class Excel
{
}

8) 当一个人向

  • COM 选项卡 上的项目添加引用
  • 而不是使用 [ComImport])时,
  • 而不是使用 Office 2007 主要互操作程序集

9) 使用COM 选项卡 添加引用与使用COM 互操作 相同,只是需要一个类型库 才能看到它?

10) Office 2003 主互操作程序集是否向后和向前兼容: - 办公室14 - 办公室2007 - 办公室2003 - 办公室XP - 办公室2000 - 97号办公室 - Office 95

如果客户和开发人员安装了新版本的 Office,它仍然可以工作吗?

11) 我们是否必须随应用程序一起运送 Office 2003 主互操作程序集?

12) 客户是否必须安装 Office 2003 主互操作程序集才能使用我们的应用程序?

13) 如果客户安装 Office 2003 主互操作程序集,他们是否必须安装 Office

14) 如果客户安装 Office 2003 主互操作程序集,他们是否必须安装 Office 2003

15) Office 2003 主互操作程序集是 Office 2003 的免费、精简版、可再发行版本吗?

16) 如果我的开发计算机装有 Office 2007,我可以使用 Office 2003 PIA 并将其交付给安装了 Office XP 的客户吗?

The technique for adding a reference to the COM interop of Office in Visual Studio is to go to:

  1. References
  2. Add Reference
  3. Select the COM tab
  4. Select Microsoft Office 11.0 Object Library

And magically named reference appears:

Microsoft.Office.Core

The Project.csproj file shows the details of the reference:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

And the project is checked into source control and all is well.


Then a developer with Office 2007 gets the project from source control, and cannot build it because such a reference doesn't exist.

He (i.e. me) checks out the .csproj file, deletes the reference to

Microsoft Office 11.0 Object Library

and re-adds the COM reference as

Microsoft Office 12.0 Object Library

And magically a named reference appears:

Microsoft.Office.Core

The Project.csproj file shows the details of the reference:

<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>4</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>

And the project is checked into source control and all is well.


Then a developer with Office 2003 gets the project from source control, and cannot build it because such a reference doesn't exist.

He (i.e. not me) checks out the .csproj file, deletes the reference to

Microsoft Office 12.0 Object Library

and re-adds the COM reference as

Microsoft Office 11.0 Object Library

And magically a named reference appears:

Microsoft.Office.Core

The Project.csproj file shows the details of the reference:

<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>3</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>

And the project is checked into source control and all is well.

Then the project is built, pressed onto CDs, and sent to the customers who have Office 2007.

And all is not well.


In the olden days (i.e. before .NET dll hell), we would reference the Office objects using a version independant ProgID, i.e.:

"Excel.Application"

which resolves to a clsid of the installed Office, e.g.

{00024500-0000-0000-C000-000000000046}    

of which a class is then constructed using a call to COM (language-netural pseudo-code):

public IUnknown CreateOleObject(string className)
{
    IUnknown unk;

    Clsid classID = ProgIDToClassID(className);
    CoCreateInstance(classID, null, 
          CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
          IUnknown, out unk);

    return unk;
}

Questions

1) What is the approved technique to automate the installed Office applications?

2) What are the Office 2003 Primary Interop Assemblies useful for?

3) If i use the Office 2003 Primary Interop Assemblies, do i have to have office 2003 installed?

4) If i build with the Office 2003 Primary Interop Assemblies, are my customers tied to Office 20003 forever?

5) Are there any Office 2007 Primary Interop Assemblies?

6) If i install the Office 2007 Primary Interop Assemblies do i have to have Office 2007 installed?

7) What is wrong with using standard COM interop to drive Excel, Word, or Outlook? e.g.:

[ComImport]
[Guid("00024500-0000-0000-C000-000000000046")]
public class Excel
{
}

8) What is one achieving when one adds a

  • Reference to items on the COM tab,
  • as opposed to using [ComImport],
  • as opposed to using the Office 2007 Primary Interop Assemblies?

9) Is adding a reference using the COM tab identical to using COM interop, except that it needs a type library before you can see it?

10) Are the Office 2003 Primary Interop Assemblies backwards and forwards compatible with:
- Office 14
- Office 2007
- Office 2003
- Office XP
- Office 2000
- Office 97
- Office 95

If a customer, and a developer, installs a new version of Office, will it still work?

11) Do we have to ship the Office 2003 Primary Interop Assemblies with our application?

12) Does the customer have to install the Office 2003 Primary Interop Assemblies before they can use our application?

13) If a customer installs the Office 2003 Primary Interop Assemblies do they have to have Office installed?

14) If a customer installs the Office 2003 Primary Interop Assemblies do they have to have Office 2003 installed?

15) Are the Office 2003 Primary Interop Assembles a free, lite, redistributable version of Office 2003?

16) If my development machine has Office 2007, can i use the Office 2003 PIAs, and ship to a customer with Office XP installed?

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

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

发布评论

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

评论(4

风启觞 2024-07-18 04:23:26

哇,这是一个巨大的问题。 我认为,一般来说,如果您的应用程序使用 PIA,那么您就假设您的目标受众安装了某个版本的 Office。 当目标用户安装 Office 时,PIA 将安装在 GAC 中。 如果他们没有安装 Office 那么您为什么要定位 Office?

是的,Office dll 是自动化 Office 的正确方法。 此处有一个程序集列表,包括一些适用于 Office 2007。

Wow that's a huge number of questions. I think that in general if your app is using the PIAs then you're assuming that your target audience has some version of Office installed. The PIAs will be installed in the GAC when the target user installs Office. If they don't have Office installed then why are you targeting Office?

Yes, the Office dlls are the correct way to automate Office. There's a list of the assemblies here, including some for Office 2007.

明媚殇 2024-07-18 04:23:26

答案是“复制本地”您为互操作获得的任何程序集 dll。 将程序集 dll 放入输出文件夹后,添加对其的引用,并将其签入源代码管理。

现在大家都有引用的程序集dll了。

The answer is to "Copy Local" whatever assembly dll you get for the interop. Once you have the assembly dll in your output folder, add a reference to it, and check it into source control.

Now everyone has the referenced assembly dll.

泅人 2024-07-18 04:23:26

你使用VSTO(Visual studio 办公工具)吗?

http://msdn.microsoft.com/en-us/office/aa905533。 ASPX

do you use VSTO (Visual studio tools for office)?

http://msdn.microsoft.com/en-us/office/aa905533.aspx

简单爱 2024-07-18 04:23:26

一个旧线程,可能大多数人都会对 CopyLocal=True 感到满意,但是这是另一种方式..
在项目文件中使用两者(或更多..?如果问题仍然存在,请考虑 Office 2010..)引用,并忽略或仅告诉 MSBuild 忽略“MSB3284”警告(未找到库) )。
因此,将其包含在您的 .csproj 文件中:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

其次:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>4</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

我很想知道 Microsoft 是否为此提供了 NuGet 库 - 只是为了让每个人都采用相同的方法。 我认为这将消除人们在网络上搜索这些答案的必要...我相信这将违反 Microsoft Office 的许可证,因此他们是唯一提供它的人。

顺便说一句,使用本地复制时,您必须小心不要错误地重新分发这些库。

An old thread, and probably most people would be happy with CopyLocal=True, however here's another way..
Use both (or more..? thinking Office 2010 if the problem still exists..) references in your project files, and either ignore or just tell MSBuild to ignore the "MSB3284" warning (Library not found).
So include this in your .csproj file:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

Followed by:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>4</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

I would be interested to see if Microsoft provides a NuGet library for this - just to get everyone on to the same approach. I think that would remove the necessity for people to search the web for these answers... I believe this would be against Microsoft Office's license so they are the only ones to supply it.

BTW with copy local you have to be careful not to redistribute these library by mistake.

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