65.9K
CodeProject 正在变化。 阅读更多。
Home

VB.NET Microsoft Outlook 标准插件创建器入门

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (6投票s)

2009年2月18日

BSD

2分钟阅读

viewsIcon

42885

downloadIcon

1286

VB.NET Microsoft Outlook 标准插件创建器入门

Click to enlarge image

引言

这篇小文章将指导您开发适用于 Microsoft Outlook 2003 的 Microsoft Visual Basic .NET 插件。

背景

众所周知,Microsoft Outlook 是一款(或就是)行业标准的电子邮件客户端。使用 VB.NET 扩展它既可以盈利,实际上也很有趣(当然是最终结果,而不是实际的编码)。

好了,让我们开始吧

首先,使用相应的向导创建一个干净的“Outlook 2003 插件”。随意命名。

然后,添加此代码来声明将要使用的对象。

Private myCommandBarControll As CommandBar
Private btnGetAppointments As CommandBarControl
Private mnuPopup As CommandBarControl  

您会注意到,对象的声明没有使用 withevents new,但我们不需要这些。稍后,我们将使用 DirectCast AddHandler 来使一切正常工作。

现在,在 ThisAddin_Startup 事件中,添加此代码

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
	ByVal e As System.EventArgs) Handles Me.Startup

Dim btnclickhandler As CommandBarButton
Dim mnuPopuphandler As CommandBarPopup
'[.....]   

同样,没有 new

为了使一切正常工作,我们必须声明一个 commandbar。所以在最后一段代码之后立即添加此代码

Dim oApp As Outlook.Application = New Outlook.Application()
myCommandBarControll = oApp.ActiveExplorer.CommandBars.Add_
	("YourCommandbar", MsoBarPosition.msoBarTop, False, True)
myCommandBarControll.Visible = True

这里唯一 new 的是 Outlook.Application() 对象。从现在开始,我们只继承。这里发生的事情是 Outlook 创建命令栏 YourCommandbar 并使其可见。但请确保您显式地将属性 Visible True 设置为 True。

它没有功能,也没有按钮,所以让我们改变一下。仍然在 ThisAddIn_Startup 中使用这段代码

mnuPopup = myCommandBarControll.Controls.Add(MsoControlType.msoControlPopup)
mnuPopup.Caption = "Your pulldown menu"
mnuPopup.Visible = True

mnuPopuphandler = DirectCast(mnuPopup, CommandBarPopup) 

这将完成在命令栏中创建标准下拉菜单的工作。请注意 directcast,我们没有使用任何 new 语句。

然而,这个下拉菜单需要一个目的。就像生活中的其他一切一样。所以,让我们在菜单中添加一个按钮。

btnGetAppointments = mnuPopuphandler.Controls.Add(MsoControlType.msoControlButton)
btnGetAppointments.Caption = "Your caption of this button"
btnGetAppointments.Visible = True

btnclickhandler = DirectCast(btnGetAppointments, CommandBarButton)
btnclickhandler.Style = MsoButtonStyle.msoButtonIconAndCaption

btnclickhandler.TooltipText = "Your tooltip text"
AddHandler btnclickhandler.Click, AddressOf OnApptClick

让我们分析一下代码。它有

  • Visible 显式地设置为 true
  • 一个 directcast,使控件对象像按钮一样工作。
  • 工具提示文本。
  • 一个样式声明设置为 MsoButtonStyle.msoButtonIconAndCaption 以使图像工作。
  • 一个 Addhandler ,用于使按钮实际执行任何操作。
  • 仍然没有 new

让我们添加一个函数,使插件实际上在用户的命令下执行任何操作。我们最终将离开 ThisAddIn_Startup 并创建一个名为 OnApptClick 的新 sub

Private Sub OnApptClick()
        MsgBox("Hello Outlook world!")
End Sub  

现在,运行你的插件。你现在完全有资格制作各种奇怪的插件了。发挥你的创造力!

如果想在按钮中使用图像,请继续阅读。

IPictureDisp

Outlook 使用 IPictureDisp 用于图像。但如果你添加一个包含以下代码的模块,那就不是什么大问题了

'
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
Imports System
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Runtime.InteropServices

Public Module PictureDispConverter

    'IPictureDisp guid
    Public iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

    'Converts an Icon into a IPictureDisp
    Public Function ToIPictureDisp(ByVal ico As Icon) As stdole.IPictureDisp

        Dim pictIcon As New PICTDESC.Icon(ico)
        Return PictureDispConverter.OleCreatePictureIndirect_
				(pictIcon, iPictureDispGuid, True)
    End Function

    'Converts an image into a IPictureDisp
    Public Function ToIPictureDisp(ByVal picture As Image) As stdole.IPictureDisp

        Dim bm As Bitmap
        If TypeOf picture Is Bitmap Then
            bm = picture
        Else
            bm = New Bitmap(picture)
        End If
        Dim pictBit As New PICTDESC.Bitmap(bm)
        Return PictureDispConverter.OleCreatePictureIndirect_
				(pictBit, iPictureDispGuid, True)
    End Function

    <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", _
				ExactSpelling:=True, PreserveSig:=False)> _
    Private Function OleCreatePictureIndirect(<MarshalAs(UnmanagedType.AsAny)> _
	ByVal picdesc As Object, ByRef iid As Guid, ByVal fOwn As Boolean) _
	As stdole.IPictureDisp
    End Function

    Private ReadOnly hCollector As New HandleCollector("Icon handles", 1000)

    'PICTDESC is a union in native, so we'll just
    'define different ones for the different types
    'the "unused" fields are there to make it the right
    'size, since the struct in native is as big as the biggest
    'union.
    Private Class PICTDESC

        'Picture Types
        Public Const PICTYPE_UNINITIALIZED As Short = -1
        Public Const PICTYPE_NONE As Short = 0
        Public Const PICTYPE_BITMAP As Short = 1
        Public Const PICTYPE_METAFILE As Short = 2
        Public Const PICTYPE_ICON As Short = 3
        Public Const PICTYPE_ENHMETAFILE As Short = 4

        <StructLayout(LayoutKind.Sequential)> _
        Public Class Icon

            Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
            Friend picType As Integer = PICTDESC.PICTYPE_ICON
            Friend hicon As IntPtr = IntPtr.Zero
            Friend unused1 As Integer = 0
            Friend unused2 As Integer = 0

            Friend Sub New(ByVal icon As System.Drawing.Icon)
                Me.hicon = icon.ToBitmap().GetHicon()
            End Sub

        End Class

        <StructLayout(LayoutKind.Sequential)> _
        Public Class Bitmap

            Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
            Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
            Friend hbitmap As IntPtr = IntPtr.Zero
            Friend hpal As IntPtr = IntPtr.Zero
            Friend unused As Integer = 0

            Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
                Me.hbitmap = bitmap.GetHbitmap()
            End Sub
        End Class

    End Class

End Module

非常感谢 http://blogs.msdn.com/rgregg/archive/2006/11/27/converting-bitmaps-to-ipicturedisp.aspx

现在,使图像类型的转换实际工作。添加了粗体行

'[....]
        btnclickhandler = DirectCast(btnGetAppointments, CommandBarButton)
        btnclickhandler.Style = MsoButtonStyle.msoButtonIconAndCaption

        btnclickhandler.Picture = ToIPictureDisp(My.Resources.comments_add)

        btnclickhandler.TooltipText = "Your tooltip text"
        AddHandler btnclickhandler.Click, AddressOf OnApptClick
'[....] 

当然,您可以将 My.Resources.comments_add 替换为您拥有的任何图标或位图。

关注点

为 Microsoft Outlook 开发令人烦恼的事情是,您必须使用 COM(无法逃避),而且调试可能很痛苦。在编写代码时,您必须知道自己在做什么。

历史

  • 2009年2月18日:首次发布
  • 2009年2月20日:添加了屏幕截图和源文件
© . All rights reserved.