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

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

问题描述


  1. 参考文献

  2. 添加参考

  3. 选择 COM 选项卡

  4. 选择 Microsoft Office 11.0对象图书馆

出现神奇名称的参考:

  Microsoft.Office.Core 

项目.csproj 文件显示参考细节:

 < COMReference Include =Microsoft.Office 。核心> 
< 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 的客户。



而且一切都不好。






在过去的日子里(ie .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 主要互操作程序集 a>?



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



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

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

8)什么是在 COM选项卡的项目中添加




  • 参考 / li>
  • 而不是使用[ComImport],

  • 而不是使用Office 2007主互操作程序集
  • ? >


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



10)Office 2003主互操作程序集是否与前端兼容:
- Office 14
- Office 2007
- Office 2003
- Office XP
- Office 2000
- Office 97
- Office 95



如果客户和开发人员安装了新版本的Office,它是否仍然ork?



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的客户端吗?

解决方案

答案是复制本地,无论您为互操作获得什么汇编数据。一旦你的输出文件夹中有程序集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天全站免登陆