Office COM add-in 开发(一)

来源:互联网 发布:软件测试报告范文 编辑:程序博客网 时间:2024/05/29 19:43

     Office 的 add-in 分为两类,一类是application add-in,一类是 COM add-in. COM add-in 是一个进程内的COM server,需要实现 IDTExensibility2 接口 。该接口定义在Msaddndr.dll (Microsoft add-in designer type library) 中,该接口有五个成员函数。

    1. OnConnection(IDispatch *pApplication, AddInDesignerObjects::ext_ConnectMode  ConnectMode, IDispatch *pAddInInst, SAFEARRAY ** custom )
        当 add-in 被加载的时候,这个函数会被触发。 如果函数返回成功,那么add-in会继续加载,如果返回失败,则add-in 会停止加载,add-in 对象会被删除。
  •            IDispatch * pApplication: reference to the host application object.
  •            AddInDesignerObjects::ext_ConnectMode: 
                                 - ext_cm_AfterStartup: The add-in is started by the end user from the COM Add-ins dialog box.
                                 - ext_cm_CommandLine: The add-in is connected from the command line. Note that this does not apply to building COM add-ins for Office applications.
                                - ext_cm_External: The add-in is connected by an external application through Automation. Note that this does not apply to building COM add-ins for Office applications.
                                 - ext_cm_Startup: The add-in is started by the host at application startup. This behavior is controlled by a setting in the registry.
  •     IDispatch *pAddInInst: A reference to the COMAddIn object that refers to this add-in in the  COMAddIns collection for the host application.  
  •     SAFEARRAY ** : An array of Variant type values that can hold user-defined data.
   2. OnDisconnection(AddInDesignerObjects::ext_DisconnectMode  RemoveMode, SAFEARRAY ** custom )
      当 add-in 被卸载的时候,这个函数会被触发。
  •      AddInDesignerObjects::ext_DisconnectMode RemoveMode: 
      - ext_dm_HostShutdown: The add-in is disconnected when the host application closes.
         - ext_dm_UserClosed: The add-in is disconnected by the end user or an Automation controller.


 3. OnStartupComplete (SAFEARRAY ** custom ):add-in 加载结束的时候会调用
     OnBeginShutdown (SAFEARRAY ** custom ) : 开始卸载 add-in 的时候会调

    COM add-in 除了普通COM 的注册的过程,还会在:HKEY_CURRENT_USER\Software\Microsoft\Office\<OfficeApp>\Addins\<ProgID> 注册。在这个键(key)下面,有一个属性 LoadBehavior 是用来定义 add-in 被加载的方式。
  • 0 = Disconnect - Is not loaded.
  • 1 = Connected - Is loaded.
  • 2 = Bootload - Load on application startup.
  • 8 = DemandLoad - Load only when requested by user.
  • 16 = ConnectFirstTime - Load only once (on next startup).
  • The typical value specified is 0x03 (Connected | Bootload). 
原创粉丝点击