自动化加载项与COM加载项 [英] Automation add-in vs. COM add-in

查看:208
本文介绍了自动化加载项与COM加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手外接程序和需要以下情形帮助:

I am a newbie with add-in programming and need help with the following scenario:

我有一个C#的Excel自动化加载调用几个 UDF 秒。我希望在自动化外接负载,其中一个屏幕,用户可以输入自己的详细信息,必须在外接负载弹出时做一个用户名和密码检查。如何使用它自动化外接办呢?

I have an C# Excel automation add-in that calls a few UDFs. I want to do a user name and password check during when the automation add-in loads for which a screen where the user can enter his details must pop up on load of the add-in. How can it be done using an automation add-in?

如何事件处理一般采用自动化加载项来完成?我希望做一些计算,当用户presses <大骨节病> F9 计算中的一些细胞的UDF公式。

How can event handling be done in general using automation addins ? I want some calculations to be done when the user presses F9 to calculate the UDF formula in some of the cells.

有没有解释事件处理的自动化外接任何物品?

推荐答案

自动化加载项,在一般情况下,是不是设计来处理这种功能。你可以有你的加载项实现IDTExtensibility2的,以获得在其中您加载运行Excel.Application对象的引用。从那里,你可以访问Excel.Application类的所有事件。但是,自动化加载项一般不设计来处理Excel对象模型事件 - 它被设计用来支持用户自定义函数(UDF)仅

Automation add-ins, in general, are not designed to handle this kind of functionality. You can have your add-in implement IDTExtensibility2 in order to get a reference to the 'Excel.Application' object in which your add-in is running. From there, you can access all the events of the 'Excel.Application' class. But an automation add-in is not generally designed to handle Excel object model events -- it is designed to support user defined functions (UDFs) only.

我想做一个用户名和密码
  自动化插件时,在检查
  负载,其中一个屏幕,在
  用户可以输入自己的细节必须弹出
  在加入负载。这怎么可能
  使用自动化插件做了什么?

I want to do a user name and password check during when the automation addin loads for which a screen where the user can enter his details must pop up on load of the add in. How can it be done using an automation addin?

当心当试图自动化加载第一负载采取行动。自动化加载项是按需加载,这意味着它没有加载,直到它被Excel的需要。当用户开始输入您的自动化加载项的第一个用户定义函数(UDF)进入细胞它通常会加载。问题是,在多数编程命令将试图在用户仍然编辑单元格来执行时失败。因此,如果你试图采取这样的行动,当你外接首先加载,有一个pretty很好的机会,它加载时Excel处于一种模式,不会让你的code到安全地执行。 (欲了解更多关于这个,请参阅:当自动化外接负载 Excel中失败)

Beware of attempting to to take action when your automation add-in first loads. An automation add-in is demand-loaded, meaning that it is not loaded until it is needed by Excel. It will typically load when the user begins entering the first user-defined function (UDF) of your automation add-in into a cell. The problem is that the majority of programmatic commands will fail when attempting to execute while the user is still editing the cell. Therefore, if you attempt to take such actions when your add-in first loads, there is a pretty good chance that it is loading while Excel is in a mode that would not allow your code to execute safely. (For more on this, see: Excel fails when Automation add-In loads.)

要解决这个问题,你可以使用托管COM加载项来代替,这是设计来处理Excel对象模型事件。您管理的COM加载项,甚至可以加载您的自动化插件,如果你想;或托管COM加载项类和自动化加载类可以驻留在同一组件内,在这种情况下,他们可以直接通信。

To get around this issue, you could use a managed COM add-in instead, which is designed to handle Excel object model events. Your managed COM add-in could even load your automation add-in, if you wanted; or the managed COM add-in class and the automation add-in class could both reside within the same assembly, in which case they could communicate directly.

如何事件处理中完成
  一般使用自动化加载项?一世
  要计算一下做时,
  用户presses F9计算
  UDF式中的一些细胞的

How can event handling be done in general using automation addins ? I want some calculations to be done when the user presses F9 to calculate the udf formula in some of the cells.

在F9键被击​​中可以通过订阅Excel.Application.SheetCalculate'事件,这将触发任何时候任何工作已经完成计算进行检测。计算,在此情况下,可能会引发因任何原因 - 不仅仅是击中F9键。如果你想陷阱F9键具体来说,那么你就需要利用Application.OnKey的回调,这是唯一可通过VBA的。你可以,不过,露出一类在项目中的COM,并让它从VBA加载项,被称为从Application.OnKey事件回叫。

Detecting when the F9 key is hit could be done by subscribing to the 'Excel.Application.SheetCalculate' event, which will fire any time any worksheet has completed calculating. Calculation, in this case, could be triggered for any reason -- not just for hitting the F9 key. If you wish to trap the F9 key specifically, then you would need to make use of the 'Application.OnKey' callback, which is only available via VBA. You could, however, expose a class in your project to COM and have it called from a VBA add-in that is called back from the 'Application.OnKey' event.

有关自动化文章插件,请参见:

For articles on automation add-ins, see:

  • Writing user defined functions for Excel in .NET
  • Writing Custom Excel Worksheet Functions in C#
  • Implementing IDTExtensibility2 in an Automation Add-in
  • Excel fails when Automation add-In loads
  • How to Deploy a .NET Automation Add-in via a Visual Studio Setup Package

有关管理的COM加载项文章,请参阅:

For articles on managed COM add-ins, see:

  • How to build an Office COM add-in by using Visual C# .NET
  • Building Microsoft Office Add-ins with Visual C# .NET and Visual Basic .NET

有关这两个COM加载项和自动化加载项文章,请参阅:

For articles regarding both COM add-ins and automation add-ins, see:

  • Excel COM add-ins and Automation add-ins
  • Build and Deploy a .NET COM Assembly

有关的文章讨论使用VBA的加载项,调用托管应用程序,请参见:

For articles discussing the use of a VBA add-in that calls your managed application, see:

  • A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?
  • How to create Excel UDFs in VSTO managed code
  • Calling a .NET Component from a COM Component

我知道,这是很多消化,但希望这可以让你去。总的来说,我会推荐管理COM加载项来处理你的用户界面功能。通过保存的数据托管COM加载项可以与自动化加载宏在轻松,无论是同时具有外接一个共同的参照装配,或同时具有托管COM加载项类和自动化添加 - 在共享在课堂上同一组件内隆重举行。

I know that this is a lot to digest, but hopefully this can get you going. Overall, I would recommend a managed COM add-in to handle your user-interface functionality. The data saved by the managed COM add-in could be shared with the automation add-in in easily, either by having both add-ins reference a common assembly, or by having both the managed COM add-in class and the automation add-in class held within the same assembly.

我希望这会有所帮助,并请询问​​如果你想我澄清任何进一步。

I hope this helps, and please ask if you would like me to clarify anything further.

迈克 -

后续回复:

非常感谢你的链接和指针。我通过链接去了,有需要做什么是公平的想法。)

Thanks a lot for the links and pointers. I went through the links and have a fair idea of what needs to be done.:)

很高兴它帮助。 :)这是一个很大的阅读,我建议你打印出来并阅读它们。您正在寻找做一些事情,是比较先进的,所以你知道的话题越多,你会更好可以。

Glad it helps. :) It's a lot of reading, I suggest you print them up and read them all. You are looking to do something that is fairly advanced, so the more you know about the topic, the better off you will be.

如果我使用的是基于COM插件来处理我的Excel事件,我该如何将我的UDF的功能呢?

If I am using a COM based addin to handle my excel events, how do I incorporate my UDF functions?

一个管理的COM加载项不能暴露UDF功能。你仍然需要一个自动化加载了点。但是,您管理的COM加载项类别以及您的自动化外接类可以同时为同一组件内,如果你想要的。因此,您的COM加载项可以要求您所需要的信息的用户,这些值将提供给自动化加载时,其中的UDF是运行。

A managed COM add-in cannot expose UDF functions. You would still need an automation add-in for that. However, your managed COM add-in class and your automation add-in class could both be within the same assembly, if you wanted. Therefore, your COM add-in could ask the user for the information that you need, and these values would be available to the automation add-in when the UDFs within it are run.

将他们暴露在公式窗格类似于使用自动化插件的情况下正常功能?

Will they be exposed as normal functions in the formula pane similar to the case of using an automation addin?

您自动化外接公开的UDF的将被自动包含在插入函数向导中,在符合您的自动化加载项的名称的类别。说明,但是,不会自动提供了Excel的内置功能包括尽可能多的信息。默认的功能通常是好的,但如果你想在插入函数向导中提供更完整的信息,这本身就是一个复杂的话题。参见:<一href=\"http://stackoverflow.com/questions/1592440/excel-2007-udf-how-to-add-function-description-argument-help\">Excel 2007 UDF:如何添加功能描述,参数说明

The UDFs exposed by your automation add-in will be included within the 'Insert Function' wizard automatically, under a category matching the name of your automation add-in. The description, however, will not automatically include as much information as is provided for Excel's built-in functions. The default functionality is usually fine, but if you wish to provide more complete information for the 'Insert Function' wizard, this is a complex topic unto itself. See: Excel 2007 UDF: how to add function description, argument help.

说我需要调用一个公式getcube返回一个数字的立方
      公共双getcube(双A){返回A * A * A; }

Say I need to call a formula getcube that returns the cube of a number public double getcube(double a) { return a * a * a; }

在我的插件同时使用,定义我的UDF和IDTExtensibility2的自定义界面,我该如何处理这种情况?能否请您用一个例子来解释这种情况?

When my addin uses both the custom interface that defines my UDFs and IDTExtensibility2, how do I handle such a case? Can you please explain this case with an example?

到目前为止,我没有看到任何需要根据你在这里展示什么来实现IDTExtensibility2的,你只需要一个标准的自动化加载项。对于标准的自动化插件,你应该在.NET中读取写作用户自定义函数为Excel 和<一个href=\"http://blogs.msdn.com/gabhan_berry/archive/2008/04/07/writing-custom-excel-worksheet-functions-in-c_2D00_sharp.aspx\">Writing自定义的Excel工作表函数在C#。有关如何实现了IDTExtensibility2的托管COM加载项的讨论,请参阅实现了IDTExtensibility2在自动化外接

So far, I don't see any need to implement IDTExtensibility2 based on what you show here, you only need a standard automation add-in. For a standard automation add-in, you should read Writing user defined functions for Excel in .NET and Writing Custom Excel Worksheet Functions in C#. For a discussion regarding how to implement IDTExtensibility2 for a managed COM add-in, see Implementing IDTExtensibility2 in an Automation Add-in.

有没有只实现在自动化插件的IDTExtensibility2的以访问Excel.Application对象的方式,或者我应该创建一个单独的COM插件呢?

Is there a way to just implement the IDTExtensibility2 on the automation addin to get access to the Excel.Application object or should I create a separate COM addin for it?

您绝对的可以的直接自动化中实现IDTExtensibility2的加载项,也没有必要创建一个托管COM加载项这一点。再次,看,实现了IDTExtensibility2在自动化外接。你想实现什么,但是,是使用事件处理针对Excel对象模型。虽然这可以用做一个自动化插件,它是非标准的,不属于那种自动化加载项是专门做的任务。特别是,你希望让用户输入信息时自动加载第一负载;这可能是一个特别棘手的情况,这就是为什么我建议你利用管理的COM加载完成这个任务。欲了解更多关于这一点,看看当自动化外接负载 Excel中失败。

You absolutely can implement IDTExtensibility2 directly within your automation add-in, there is no need to create a managed COM add-in for this. Again, see, Implementing IDTExtensibility2 in an Automation Add-in. What you wish to achieve, however, is to use event handling against the Excel object model. While this could be done using an automation add-in, it is non-standard and is not the kind of task that an automation add-in is designed to do. In particular, you wish to have the user input information when the automation add-in first loads; this can be a particularly tricky situation, which is why I am recommending that you utilize a managed COM add-in for this task. For more on this, see Excel fails when Automation add-In loads.

要澄清,管理的COM加载项和自动化加载项仅仅是已取得COM可见,并正确注册类。没有理由,为什么这两个类不能在同一个组件内的存在。而且,因为它听起来像你希望你的功能,包括UDF和Excel对象模型事件处理,既包括管理的COM加载项和自动化加载项将使您能够处理所有你所寻求的功能的单个组件该Excel中预计的方式。

To clarify, managed COM add-ins and automation add-ins are merely classes that have been made COM-visible and are registered correctly. There is no reason why these two classes cannot exist within the same assembly. And, since it sounds like you want your functionality to include both UDFs and Excel object model event handling, a single assembly that includes both a managed COM add-in and an automation add-in would enable you to handle all the functionality you seek in the manner that Excel expects.

我知道,这是很多消化,但如果你仔细看,我建议在这里的文章,我认为它才有意义......

I know that this is a lot to digest, but if you carefully read the articles that I suggested here, I think that it will make sense...

迈克

这篇关于自动化加载项与COM加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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