使用 COM 加载项自定义 OutlookCustomizing Outlook using COM add-ins

创建 COM 加载项包括以下两个主要步骤:Creating a COM add-in involves two major steps:

  1. 在动态链接库 (DLL) 的类模块中实现 IDTExtensibility2 接口。Implement the IDTExtensibility2 interface in a class module of a dynamic link library (DLL).
  2. 注册 COM 加载项。Register the COM add-in.

实现 IDTExtensibility2 接口Implement the IDTExtensibility2 Interface

IDTExtensibility2 接口由五个事件过程组成。The IDTExtensibility2 interface consists of five event procedures. 要在 Visual Basic 程序中实现此接口,请设置对 Microsoft Add-In Designer 对象库的引用,然后将以下语句添加到类模块的声明部分中:To implement this interface in a Visual Basic program, set a reference to the Microsoft Add-In Designer object library and then add the following statement to the Declarations section of a class module:

Implements IDTExtensibility2

然后,您可以在该类模块的代码窗口中添加空的事件过程,并将自己的程序代码添加到过程中。You can then add the empty event procedures to the code window of the class module and add your own program code to the procedures. 也可以从 Outlook COM 加载项模板复制空的过程。You can also copy the empty procedures from an Outlook COM Add-in Template.

注册 COM 加载项Register the COM add-in

为了在 Outlook 中使用,必须注册加载项 DLL。In order to work with Outlook, the add-in DLL must be registered. 该 DLL 的类 ID 在注册表的 \HKEY_CLASSES_ROOT 子树下注册。The DLL's class ID is registered beneath the \HKEY_CLASSES_ROOT subtree in the registry.

另外,必须在注册表中添加有关加载项的信息。In addition, information about the add-in must be added to the registry. 此信息提供加载项的名称、说明、目标应用程序、初始加载行为和连接状态。This information provides the add-in's name, description, target application, initial load behavior, and connection state.

注释 如果使用 Microsoft Visual Basic 6.0 或更高版本的开发人员设计 COM 加载项,加载项设计器将执行注册 COM 加载项所需的步骤。Note If you use Microsoft Visual Basic 6.0 or later Developer to design your COM add-in, the add-in designer will perform the steps required to register the COM add-in for you.

下面的示例显示一个示例注册表编辑器文件 (.reg) 的内容,演示如何注册 Outlook COM 加载项。The following example shows the contents of a sample registry-editor (.reg) file that illustrates how to register an Outlook COM add-in.

   [HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\SampleAddIn.AddInIFace]
    "FriendlyName"="Sample Add-in"
    "Description"="Sample Outlook Add-In"
    "LoadBehavior"=dword:00000008

当第一次注册 COM 加载项时,LoadBehavior 可以设置为以下任一标志。When the COM add-in is first registered, LoadBehavior can be set to any of the following flags.

Value 说明Description
2 2 启动时加载。Load at startup. COM 加载项在 Outlook 启动时加载并连接。The COM add-in is to be loaded and connected when Outlook starts.
8 8 需要时加载。Load on demand. COM 加载项在用户要求时才加载和连接,例如使用****“COM 加载项”对话框进行加载和连接。The COM add-in is to be loaded and connected only when the user requests it, such as by using the COM Add-ins dialog box.
16 16 第一次运行时连接。Connect first time. 在 COM 加载项注册后用户第一次运行 Outlook 时加载和连接。The COM add-in is loaded and connected the first time the user runs Outlook after the COM add-in has been registered. 以后 Outlook 运行时,COM 加载项将在用户需要时加载。The next time Outlook is run, the COM add-in is loaded when the user requests it. 如果 COM 加载项修改用户界面以允许用户按需请求连接 COM 加载项(例如,通过单击某个按钮),则使用此值。Use this value if your COM add-in modifies the user interface to allow the user to request the COM add-in be connected on demand (by clicking a button, for example).

在 COM 加载项注册并且加载后,LoadBehavior 值可与下面两个标志中的任何一个结合使用,以表明 COM 加载项的当前连接状态。After the COM add-in is registered and loaded, the LoadBehavior value can be combined with either of the following two flags to indicate current connection state of the COM add-in.

FlagFlag 说明Description
00 已断开连接Disconnected
1 1 已连接Connected

要连接 COM 加载项,请设置 LoadBehavior 中的"已连接"标志;要断开与 COM 加载项的连接,请清除该标志。To connect the COM add-in, set the Connected flag in LoadBehavior; clear the flag to disconnect the COM add-in.

FriendlyName 值指定在 "COM 加载项" 对话框中显示的 COM 加载项的名称。The FriendlyName value specifies the name of the COM add-in as it's displayed in the COM Add-in dialog box. Description 值提供有关 COM 加载项的其他信息。The Description value provides additional information about the COM add-in.

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.