在Outlook中添加自己的菜单


在Outlook中添加自己的菜单
    在Microsoft Office 2000中有一个新的功能:COM加载项(COM Add-In)。利
用COM加载项,我们可以通过编写COM组件实现将自己的软件集成在Office系列产品
中。例如金山词霸.Net就是利用COM加载项实现添加按钮到Word中。
    下面的文章将通过如何通过VB编程实现添加按钮到OutLook象大家介绍如何实
现COM加载项。要在VB中建立COM加载项,首先需要建立一个ActiveX DLL工程,然后
在工程中建立一个类(Class),在类代码中实现Office规定的IDTExtensibility2
接口。如果有读者做过或者接触过Shell扩展编程(Shell Extension)和接口
(Interface)的话,这些可能都很好理解。如果没有接触过。可以将我们的COM加载
项看成服务器,Outlook作为客户,客户如何调用服务器的功能呢?这就需要有一个
规范,或者称为协议。服务器只要实现了协议规定的代码,客户就可以直接通过协
议来调用服务器程序了,而不用管服务器是如何实现协议的,而Windows编程中接口
就是这样的协议。
    下面来介绍如何实现COM加载项。首先建立一个新的 ActiveX DLL工程,VB会
自动添加一个Class Module:Class1,将Class1的Name属性改为clsOutLook,将工程
的Name属性改为MyOutlook,然后保存工程。
    要实现IDTExtensibility2接口,必须引入对IDTExtensibility2接口的定义。另
外我们还需要引入Office以及Outlook库,点击菜单 Project | References 项,在
引用列表中选中如下的三项:
	Microsoft Add-in Desing
	Microsoft Outlook 9.0 Object Library
	Microsoft Office 10.0 Object Library
	如下图所示:
 


	然后打开clsOutLook,在其中添加如下代码:
	
Option Explicit

Implements IDTExtensibility2

Dim WithEvents objApp As Outlook.Application
Dim objButton As Office.CommandBarButton
Dim objBar As Office.CommandBars
Dim objMenuBar As Office.CommandBar
Dim objFolder As Outlook.MAPIFolder

Dim WithEvents objMyButton As Office.CommandBarButton
Dim WithEvents objResetBar As Office.CommandBarButton
Dim WithEvents objNameSpace As Outlook.NameSpace

Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
    'MsgBox "执行 "
End Sub

Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
    MsgBox "对象将被关闭!"
    
    Set objApp = Nothing
    Set objBar = Nothing
    Set objButton = Nothing
    Set objFolder = Nothing
    Set objMenuBar = Nothing
    Set objMyButton = Nothing
    Set objNameSpace = Nothing
    Set objResetBar = Nothing
End Sub

Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode _
    As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)

    Dim objMyControl As Object
    
    MsgBox "连接到 Add-ins对象上"
    ' 获得Outlook的 Application 对象.
    Set objApp = Application
    ' 获得OutLook的MAPI NameSpace Namespace.
Set objNameSpace = objApp.GetNamespace("MAPI")
'弹出文件夹选择框
    Set objFolder = objNameSpace.PickFolder()
    
    '添加及工具栏
    Set objBar = objApp.ActiveExplorer.CommandBars
    Set objMenuBar = objBar.Add("我的菜单", , True, True)
    objMenuBar.Visible = True
    
    '添加主菜单
    Set objMyControl = _
        objMenuBar.Controls.Add(msoControlPopup, , , , True)
    objMyControl.Caption = "&Menu Item"
    
'    添加菜单项
    Set objResetBar = objMyControl.Controls.Add( _
        Type:=msoControlButton, Temporary:=True, Before:=1)
    objResetBar.Caption = "&Reset Menu"
    objResetBar.Enabled = True
    
    Set objMyButton = objMyControl.Controls.Add( _
        Type:=msoControlButton, Temporary:=True, Before:=1)
    objMyButton.Caption = "&Test Menu"
    objMyButton.Enabled = True

End Sub

Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As _
    AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
    '
End Sub

Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
    '
End Sub

Private Sub objApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim Prompt As String
    
    Prompt = "你确定要发送邮件: " & Item.Subject & "吗?"
    If MsgBox(Prompt$, vbYesNo + vbQuestion, "") = vbNo Then
        Cancel = True
    End If
End Sub


Private Sub objResetBar_Click(ByVal Ctrl As Office.CommandBarButton, _
    CancelDefault As Boolean)
    objMenuBar.Delete
End Sub

	保存工程,然后编译到MyOutlook.dll,COM加载项程序的代码就完成了。接下来需要
注册组件,Windows提供了regsvr32.exe供注册组件,方法是在Dos命令行下敲入:

regsvr32 MyOutlook.dll

系统就将MyOutlook添加到注册表中,打开注册表编辑器,可以在注册表中找到如下的注册项:

 

注:上图中左边列表中的Guid同你生成的会有不同,这个Guid是系统随即产生的。

打开文本编辑器,输入如下的内容:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER/Software/Microsoft/Office/Outlook/Addins/MyOutlook.clsOutlook]
"CommandLineSafe"=dword:00000000
"Description"="Outlook samples"
"FriendlyName"="Outlook samples"
"LoadBehavior"=dword:00000003

将上面输入的内容保存到以reg为后缀的文件,然后双击将其合并到注册表中。

COM加载项现在已经全部建立完毕并且连接到Outlook中了。打开Outlook,会弹出消息框提示:
"连接到 Add-ins对象上",然后弹出文件夹选择框。在outlook窗口中,可以看到多了一个菜单项,
如下图所示:
 


    现在建立一个新邮件,然后发送,系统会提示你:"你确定要发送邮件: 吗?"。点击取消,
将不会发送邮件。

    现在来看上面的代码,在代码中我们实现了IDTExtensibility2接口,Outlook在启动时会调用接
口的OnConnection方法,将自身的Application对象作为参数ByVal Application As Object传递过去
我们只要在实现OnConnection方法时获得Application对象就可以访问Outlook中的诸如工具栏、文件
夹、联系人等对象了。
    在Outlook关闭时会调用OnBeginShutdown方法,我们需要在实现该方法的代码中释放建立的所有
对象。IDTExtensibility2接口的另外三个方法我们不需要实现,但是代码要写在那里。否则程序会无
法编译通过。要断开COM加载项与Outlook的连接,我们只要打开注册表编辑器,转到下面的项:

HKEY_CURRENT_USER/Software/Microsoft/Office/Outlook/Addins

将其下的MyOutlook.clsOutlook项删除就可以了。

以上程序在Win2000中文版、Office 2000中文版、VB6下编译运行通过

 
在 VSTO 添加鼠标右键菜单,可以通过以下步骤实现: 1. 在 Visual Studio 创建一个新的 VSTO 项目。 2. 在解决方案资源管理器,右键单击项目,并选择“添加”->“新项”。 3. 在“添加新项”对话框,选择“Office 项目”->“Outlook 项”,并选择“自定义 Outlook 邮件项”。 4. 在“自定义 Outlook 邮件项”对话框,选择“邮件”并点击“下一步”。 5. 在“选择项类型”页面,选择“邮件窗体”,并点击“下一步”。 6. 在“命名项”页面,为新项命名并点击“完成”。 7. 打开该项的代码视图,在 ThisAddIn.cs 文件添加以下代码: ``` private void ThisAddIn_Startup(object sender, System.EventArgs e) { Application.ItemContextMenuDisplay += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay); } void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection) { if (Selection.Count == 1 && Selection[1] is Outlook.MailItem) { CommandBarPopup popup = (CommandBarPopup)CommandBar.Controls.Add(MsoControlType.msoControlPopup, missing, missing, missing, true); popup.Caption = "My Custom Menu"; CommandBarButton button = (CommandBarButton)popup.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true); button.Caption = "My Custom Command"; button.Click += new _CommandBarButtonEvents_ClickEventHandler(button_Click); } } void button_Click(CommandBarButton Ctrl, ref bool CancelDefault) { MessageBox.Show("My Custom Command was clicked"); } ``` 上述代码,我们通过 ThisAddIn_Startup 事件处理程序,订阅了 Application.ItemContextMenuDisplay 事件。在 Application_ItemContextMenuDisplay 事件处理程序,我们检查当前选择是否为邮件,并创建一个自定义弹出菜单和一个自定义命令按钮。当用户单击自定义命令按钮时,会触发 button_Click 事件处理程序,弹出一个消息框。 现在,您可以运行该项目,打开 Outlook 并尝试右键单击邮件,您将看到自定义的菜单和命令按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值