天天看點

Outlook add-in 插件.Net開發經驗-補充(1)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

in: 12pt 0cm 3pt">Outlook add-in 插件.Net開發經驗-補充(1)

in: 0cm 0cm 0pt">這次補充了些東西,把原文擴充了一些,為了看着友善,直接合并在了一起。

in: 0cm 0cm 0pt">  

in: 0cm 0cm 0pt">第一次寫這玩意,記錄些開發中的過程,.Net開發設定要比在VB裡複雜一些,要把office的對象用ms提供的工具包裝一下,才能在.Net開發環境裡使用。

ing8 style="MARGin: 12pt 0cm 3.2pt">開發環境設定:

indent2 style="MARGin: 0cm 0cm 6pt 21pt">我的開發環境:Windows2K professional + Office XP

indent2 style="MARGin: 0cm 0cm 6pt 21pt">Office每個版本的對象,不盡相同,一般新版本相容舊版本,也有可能會廢棄某些對象的接口。我看過Office XP,Office2000的Outlook對象,Outlook XP比Outlook2000要多出一些對象和接口函數。

indent2 style="MARGin: 0cm 0cm 6pt 21pt">OfficeXP需要配置ms提供的設定檔案,具體可看 instyle="miter"> ineDrawn pixelL ineWidth 0"> (indent2 style="MARGin: 0cm 0cm 6pt 21pt">Office 2003 直接在Office安裝包裡,安裝.Net開發支援,直接可以在VS.Net裡開發了。

indent2 style="MARGin: 0cm 0cm 6pt 21pt">我使用VB.Net開發,這樣直接對Outlook對象程式設計比較友善(使用withevents),用C#的話,需要自定義事件參數,設定delegate。

indent2 style="MARGin: 0cm 0cm 6pt 21pt">對Outlook進行二次開發,可能用到CDO1.21對象,這個不是必需的,按個人需要。使用CDO1.21對象,可以使用一些較為核心的Outlook方法。這些方法,屬性在Outlook對象裡可能無法使用。

indent2 style="MARGin: 0cm 0cm 6pt 21pt">如果需要更直接的設定,擷取Outlook對象,可以對MAPI32.dll提供的api接口,進行控制,可以最大限度的操縱Outlook。這些接口直接使用C++程式設計最友善,我沒有具體的做過,隻測試了一些,也就不多說了。

ing8 style="MARGin: 12pt 0cm 3.2pt">開發過程簡介:

in: 0cm 0cm 0pt">       在vs.Net裡,其他項目ingdings; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-char-type: symbol; mso-symbol-font-family: Wingdings">ingdings">à 擴充性項目 ingdings; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-char-type: symbol; mso-symbol-font-family: Wingdings">ingdings">à 共享的外接程式。

in: 0cm 0cm 0pt">

in: 0cm 0cm 0pt">  

in: 0cm 0cm 0pt">選擇語言:

in: 0cm 0cm 0pt">

in: 0cm 0cm 0pt">  

in: 0cm 0cm 0pt">選擇外接程式的加載到的主程式,這裡我隻選擇Outlook:

in: 0cm 0cm 0pt">

in: 0cm 0cm 0pt">  

in: 0cm 0cm 0pt">程式會幫我們自動生成一個addin項目,包含一個Connect.vb檔案,打開看看。

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">Implementsing: 0pt; mso-hansi-font-family: 'Times New Roman'"> Extensibility.IDTExtensibility2 的接口。

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">象這樣,我們就可以取得Outlook的application對象,操縱Outlook。

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

in: 0cm 0cm 0pt">

ing: 0pt; mso-hansi-font-family: 'Times New Roman'">取得Outlook的application對象,操縱Outlook

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">Diming: 0pt; mso-hansi-font-family: 'Times New Roman'"> m_oaddin As oladdin

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">Publicing: 0pt; mso-hansi-font-family: 'Times New Roman'"> 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

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Dim oApp As myOutlook.Application

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Dim oType As Type

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Dim GetProgID As Object

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Dim MyProgID As String

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Dim oArgs As Object()

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">           

in: 0cm 0cm 0pt 63pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">Try

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                m_oaddin = New oladdin

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                'Use invokeMember to get ProgID of addininst object

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                oType = addininst.GetType

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                GetProgID = oType.invokeMember("ProgID", _

in: 0cm 0cm 0pt 168pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">BindingFlags.Public Or BindingFlags.GetField Or BindingFlags.GetProperty, _

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                            Nothing, _

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                                addininst, _

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                            oArgs)

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                MyProgID = CType(GetProgID, String)

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                oApp = CType(application, myOutlook.Application)

in: 0cm -34.65pt 0pt 0cm; TEXT-ALIGN: left; mso-para-margin-right: -3.3gd; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                'Don't call initHandler if Explorers.Count = 0 and inspectors.Count = 0

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                If oApp.Explorers.Count = 0 And oApp.inspectors.Count = 0 Then

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                    Exit Sub

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                End If

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                ' initialize COMaddin object with this connect object to allow

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                ' external clients to get access to exposed features

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                oApp.COMaddins.Item(MyProgID.ToString).Object = Me

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                'Call initHandler

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">                m_oaddin.initHandler(oApp, MyProgID)

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            Catch ex As SystemException

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">             

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">            End Try

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">Ending: 0pt; mso-hansi-font-family: 'Times New Roman'"> Sub

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

in: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left>ing: 0pt; mso-hansi-font-family: 'Times New Roman'">當然也可以把所有的對Outlook的操作,放在一個單獨的類中處理:m_oaddin.initHandler(oApp, MyProgID)

in: 0cm 0cm 0pt">ing: 0pt; mso-hansi-font-family: 'Times New Roman'">  

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

繼續閱讀