Outlook API
1、簡介
若要從Outlook 外控制Outlook對象,必須在編寫代碼的工程中建立對Outlook對象庫的引用。
1.1 Outlook Application說明:
代表整個Microsoft Outlook應用程序。它是層次結構中唯一可使用CreateObject方法或GetObject函數返回的對象。
1.2 Outlook Application 對象的用途:
- 作為根對象,使用它可訪問 Outlook 層次結構中的其他對象。
- 允許直接訪問使用CreateItem創建的新項目,而不用遍歷對象層次結構。
- 允許訪問當前界面對象(瀏覽器和檢查器)。
1.3 返回 Outlook Application 對象引用的方法:
- 可以使用 CreateObject 函數啟動新的Outlook 會話,並且返回Application對象的引用,該對象代表新會話。
- 可以使用GetObject函數返回Application對象的引用,該對象代表正在運行的會話。請註意,因為在任何給定時刻只能有一個Outlook實例處於運行狀態,所以GetObject在Outlook中使用時無太大用途。CreateObject總是用來訪問當前Outlook實例或在沒有實例時創建新實例。但是,也可以使用GetObject方法的錯誤跟蹤功能來確定Outlook當前是否處於運行狀態。
- 可以在幾種類型的語句中使用 New關鍵字隱式地創建Outlook Application對象的新實例
若要啟動Outlook自動化會話,可以用早綁定或晚綁定。晚綁定使用GetObject或CreateObject函數初始化Outlook。例如,以下代碼將對象變量設置為Outlook Application對象,該對象為Outlook對象模型中的最高層對象。所有自動化代碼都必須首先定義Outlook Application對象,才能夠訪問其他Outlook對象。
Dimol as Object Set ol = CreateObject("Outlook.Application")
若要使用早綁定,首先要設置到Outlook對象庫的引用。然後就可用以下語法啟動Outlook會話。
Dim ol as Outlook.Application Set ol = New Outlook.Application
大部分編程解決方案都與 Outlook 中存儲的數據進行交互。Outlook在郵件應用程序編程接口(MAPI)文件夾中存儲其全部信息。在將對象變量設置為Outlook Application對象後,通常要設置一個 Namespace對象來引用 MAPI,如下所示:
Set ol = New Outlook.Application Set ns = ol.GetNameSpace("MAPI") Set f = ns.GetDefaultFolder(olFolderContacts)
2、訪問Outlook
VBA包含四種從另一個程序中訪問Outlook的方法。
2.1.1 Outlook未被加載:CreateObject方法
Sub GetOlObject_1() Dim ol As Object, counter As Integer Set ol = CreateObject("Outlook.Application") counter = ol.Getnamespace("MAPI").Getdefaultfolder(6).Items.Count Debug.Print "InBox中郵件的總數為:"; counter End Sub
限制條件:CreateObject不能識別Outlook類型名稱,只能識別Outlook常量。
例如:在VBA中,"收件箱"映射的類型名稱是olFolderInbox,映射的Outlook常量是6。
counter = CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.Count
將返回一個錯誤。
2.1.2 Outlook已經被加載:GetObject方法
Sub GetOlObject_2() Dim ol As Object, counter As Integer Set ol = GetObject(, "Outlook.Application") counter = ol.Getnamespace("MAPI").Getdefaultfolder(6).Items.Count Debug.Print "InBox中郵件的總數為:"; counter End Sub
GetObject方法的限制條件同CreateObject。
2.1.3 Outlook
Sub GetOlObject_3() Dim ol As Outlook.Application, counter As Integer Set ol = New Outlook.Application counter = ol.GetNamespace("MAPI").GetDefaultFolder(6).Items.Count Debug.Print "3InBox中郵件的總數為:"; counter End Sub
2.1.4 加載Outlook_VBA_Library
References方法:無論Outlook是否被加載都獨立。
手動:VBE-->工具-->引用-->Microsoft Outlook 11.0 Object Library
Sub GetOlRef() ThisWorkbook.VBProject.References.AddFromFile "msoutl9.olb" ‘Outlook 2000 ThisWorkbook.VBProject.References.AddFromFile "msoutl10.olb" ‘Outlook 2003 ThisWorkbook.VBProject.References.AddFromFile "msoutl11.olb" ‘Outlook 2007 End Sub
加載庫後,你可以使用Outlook作為一個對象。
counter = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.Count
此種情況下,你可以使用Outlook的類型名稱和常量。
Sub GetOlObject_4() Dim counter As Integer counter = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.Count Debug.Print "InBox中郵件的總數為:"; counter End Sub
1.2 Outlook中的默認文件夾
1.2.1 DefaultFolder的清單
Sub ol_5() Dim ol As Object Set ol = CreateObject("Outlook.Application") With ol.GetNamespace("MAPI") Debug.Print .GetDefaultFolder(3).Name ‘刪除的項目Deleted items Debug.Print .GetDefaultFolder(4).Name ‘發件箱PostOut Debug.Print .GetDefaultFolder(5).Name ‘發送項目Sent items Debug.Print .GetDefaultFolder(6).Name ‘收件箱PostIn Debug.Print .GetDefaultFolder(9).Name ‘日歷Canlender Debug.Print .GetDefaultFolder(10).Name ‘聯系人Contacts Debug.Print .GetDefaultFolder(11).Name ‘日記Journals Debug.Print .GetDefaultFolder(12).Name ‘便簽Notes Debug.Print .GetDefaultFolder(13).Name ‘任務Tasks Debug.Print .GetDefaultFolder(14).Name ‘提醒Reminders Debug.Print .GetDefaultFolder(15).Name ‘提醒Reminders Debug.Print .GetDefaultFolder(16).Name ‘草稿Drafts End With End Sub
OlDefaultFolders常量 | 值 |
---|---|
olFolderCalendar | 9 |
olFolderContacts | 10 |
olFolderDeletedItems | 3 |
olFolderDrafts | 16 |
olFolderInbox | 6 |
olFolderJournal | 11 |
olFolderJunk | 23 |
olFolderNotes | 12 |
olFolderOutbox | 4 |
olFolderSentMail | 5 |
olFolderTasks | 13 |
olPublicFoldersAllPublicFolders | 18 |
olFolderConflicts | 19 |
olFolderLocalFailures | 21 |
olFolderServerFailures | 22 |
olFolderSyncIssues | 20 |
1.3 Outlook的標準項目
標準的項目有以下幾種:電子郵件(email)、約會(appointment)、聯系人(contact)、任務(task)、日記(journal)、便簽(note)、‘sticker‘(Post-it)、distributionlist
特殊項目:taskrequest、meetingrequest
Outlook根據存儲的文件夾區分郵件:
草稿郵件:草稿文件夾-->GetDefaultFolder(16)
郵件:映射到PostOut-->GetDefaultFolder(4)
發送郵件:映射到Sent items-->GetDefaultFolder(5)
接收郵件:映射到PostIn-->GetDefaultFolder(6)
1.3.1 標準項目清單:
Outlook API