如何使用Office从Visual Studio C#? [英] How to use Office from Visual Studio C#?

查看:387
本文介绍了如何使用Office从Visual Studio C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 引用

  2. 添加参考

  3. 选择 COM 选项卡

  4. 选择 Microsoft Office 11.0 Object图书馆

出现神奇的命名参考:

  Microsoft.Office.Core 

.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文件,删除对

的引用

  Microsoft Office 11.0对象库

并将COM引用重新添加为



  Microsoft Office 12.0对象库

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

  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对象库

并将COM引用重新添加为

  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>

将项目签入源代码管理,一切正常。



然后建立项目,将其压入 CD ,并发送给具有 Office 2007 的客户。


$





$ b

在过去的日子里(也就是.NET dll hell之前) ,我们将使用版本独立的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标签上添加项目时添加





  • ,而不是使用 Office 2007主互操作装置,而不是使用[ComImport],

  • >


9)使用与 COM interop 相同的 COM选项卡只要它需要类型库才能看到它?



10)Office 2003主互操作程序集与之前兼容:
- Office 14
- Office 2007
- Office 2003
- Office XP
- Office 2000
- Office 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,

解决方案

答案是复制本地无论什么程序集dll你得到的互操作。一旦你的输出文件夹中有汇编dll,添加一个引用,并检查它到源代码控制。



现在每个人都有引用的程序集dll。


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?

解决方案

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.

这篇关于如何使用Office从Visual Studio C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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