Outlook add-in 插件.Net开发经验

类别:.NET开发 点击:0 评论:0 推荐:

Outlook add-in 插件.Net开发经验

第一次写这玩意,记录些开发中的过程,.net开发设置要比在VB里复杂一些,要把office的对象用ms提供的工具包装一下,才能在.Net开发环境里使用。

开发环境设置:

我的开发环境:Windows2K professional + Office XP

Office每个版本的对象,不尽相同,一般新版本兼容旧版本,也有可能会废弃某些对象的接口。我看过Office XPOffice2000outlook对象,Outlook XPOutlook2000要多出一些对象和接口函数。

OfficeXP需要配置ms提供的设置文件,具体可看 (下载)

我使用VB.Net开发,这样直接对Outlook对象编程比较方便(使用withevents),用C#的话,需要自定义事件参数,设置delegate

Outlook进行二次开发,可能用到CDO1.21对象,这个不是必需的,按个人需要。使用CDO1.21对象,可以使用一些较为核心的Outlook方法。这些方法,属性在Outlook对象里可能无法使用。

如果需要更直接的设置,获取Outlook对象,可以对MAPI32.dll提供的api接口,进行控制,可以最大限度的操纵Outlook。这些接口直接使用C++编程最方便,我没有具体的做过,只测试了一些,也就不多说了。

开发过程简介:

       vs.net里,其他项目à 扩展性项目 à 共享的外接程序。

选择语言:

选择外接程序的加载到的主程序,这里我只选择outlook

程序会帮我们自动生成一个Addin项目,包含一个Connect.vb文件,打开看看。

Implements Extensibility.IDTExtensibility2 的接口。

象这样,我们就可以取得outlook的application对象,操纵Outlook。

 

Dim m_oAddin As olAddin

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

 

            Dim oApp As myOutlook.Application

            Dim oType As Type

            Dim GetProgID As Object

            Dim MyProgID As String

            Dim oArgs As Object()

           

Try

                m_oAddin = New olAddin

                'Use InvokeMember to get ProgID of addInInst object

                oType = addInInst.GetType

                GetProgID = oType.InvokeMember("ProgID", _

BindingFlags.Public Or BindingFlags.GetField Or BindingFlags.GetProperty, _

                            Nothing, _

                                addInInst, _

                            oArgs)

                MyProgID = CType(GetProgID, String)

                oApp = CType(application, myOutlook.Application)

                'Don't call InitHandler if Explorers.Count = 0 and Inspectors.Count = 0

                If oApp.Explorers.Count = 0 And oApp.Inspectors.Count = 0 Then

                    Exit Sub

                End If

                ' Initialize COMAddin object with this connect object to allow

                ' external clients to get access to exposed features

                oApp.COMAddIns.Item(MyProgID.ToString).Object = Me

                'Call InitHandler

                m_oAddin.InitHandler(oApp, MyProgID)

            Catch ex As SystemException

             

            End Try

End Sub

 

当然也可以把所有的对outlook的操作,放在一个单独的类中处理:m_oAddin.InitHandler(oApp, MyProgID)

 

一些注意点:

       具体要做些什么,我也不仔细说了,察看msdn就明白了。

       我在开发过程中的一些注意,列一下:

在单独类oAddin中处理,加入

1.       <GuidAttribute("CA940FE1-1193-411b-82DF-570A04491150"), ProgIdAttribute("AddinForBible.olAddin")>

Public Class OutAddIn

End Class

 

2.       清除outlook的对象:

Public Sub DisposeObject(ByVal obj As Object)

            'Wraps ReleaseCOMObject to provide a 'safe' disposal helper method.

            Dim count As Integer

            Try

                If obj Is Nothing Then

                    Exit Try

                End If

                If Not Marshal.IsComObject(obj) Then

                    Exit Try

                End If

                count = Marshal.ReleaseComObject(obj)

                While count > 0

                    count = Marshal.ReleaseComObject(obj)

                End While

            Catch ex As SystemException

            Finally

                obj = Nothing

            End Try

End Sub

 

3.        CDO1.21对象在Office的安装包里可以安装,使用例子:

设置folder的default post message 属性为 自定义 form

这个属性找了很久,msdn里没有提及,留在这里吧,以后也不太会再作outlook开发了

 

g_olNamespace = m_olOutlookApp.Session

     g_olNamespace.Logon(, , False, False)

     g_objMAPISession = New MAPI.Session

g_objMAPISession.Logon(, , False, False)

Public Function SetFolderDftMsgPostClass(ByVal sFolderID As String, ByVal sStoreID As String) As Boolean

 

            Dim objMAPIFolder As MAPI.Folder

            Dim objMAPIFields As MAPI.Fields

            Const PR_DEF_POST_DISPLAYNAME = &H36E6001E '定制form的显示名字

            Const PR_DEF_POST_MSGCLASS = &H36E5001E  '定制form的MessageClass名称

            Try

                objMAPIFolder = g_objMAPISession.GetFolder(sFolderID, sStoreID)

                objMAPIFields = objMAPIFolder.Fields

                Try

                    If objMAPIFields.Item(PR_DEF_POST_MSGCLASS).value = cVerseMessageClass Then

                        Exit Try

                    End If

                Catch ex As Exception

                    With objMAPIFields

                        .Add(PR_DEF_POST_DISPLAYNAME, cVerseFormName)

                        .Add(PR_DEF_POST_MSGCLASS, cVerseMessageClass)

                    End With

                    objMAPIFolder.Update()

                End Try

            Catch ex As Exception

            Finally

            End Try

            DisposeObject(objMAPIFields)

            DisposeObject(objMAPIFolder)

End Function

 

 

4.         其他零零碎碎的也满多的,不再罗嗦了。

 

本文地址:http://com.8s8s.com/it/it45194.htm