一、Outlook简介编程
若要从Outlook 外控制Outlook对象,必须在编写代码的工程中创建对Outlook对象库的引用。浏览器
1.1 Outlook Application说明:app
表明整个Microsoft Outlook应用程序。它是层次结构中惟一可以使用CreateObject方法或GetObject函数返回的对象。函数
1.2 Outlook Application 对象的用途:工具
1.3 返回 Outlook Application 对象引用的方法:spa
若要启动Outlook自动化会话,可使用前期绑定或后期绑定。后期绑定使用GetObject或CreateObject函数初始化Outlook。例如,如下代码将对象变量设置为Outlook Application对象,该对象为Outlook对象模型中的最高层对象。全部自动化代码都必须首先定义Outlook Application对象,才可以访问其余Outlook对象。code
Dim ol as Object/Variant Set ol = CreateObject("Outlook.Application")
若要使用前期绑定,首先要设置到Outlook对象库的引用。而后就可用如下语法启动Outlook会话。对象
Dim ol as Outlook.Application Set ol = New Outlook.Application 或直接使用: Dim ol as New Outlook.Application
大部分编程解决方案都与 Outlook 中存储的数据进行交互。Outlook在邮件应用程序编程接口(MAPI)文件夹中存储其所有信息。在将对象变量设置为Outlook Application对象后,一般要设置一个 Namespace对象来引用 MAPI,以下所示:blog
Set ol = New Outlook.Application Set ns = ol.GetNameSpace("MAPI") Set f = ns.GetDefaultFolder(olFolderContacts)
二、访问Outlook接口
2.1 VBA包含3种从另外一个程序中访问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_VBA_Library
References方法:不管Outlook是否被加载都独立。
手动引用:VBE-->工具-->引用-->Microsoft Outlook 11.0/12.0/15.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_3() Dim counter As Integer counter = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.Count Debug.Print "InBox中邮件的总数为:"; counter End Sub
或者使用New关键字隐式地建立Outlook对象:
Sub GetOlObject_4() 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 获取动态引用:
Sub GetRefInfo() Dim i As Integer On Error Resume Next For i = 1 To ThisWorkbook.VBProject.References.Count Debug.Print ThisWorkbook.VBProject.References.Item(i).Name Debug.Print ThisWorkbook.VBProject.References.Item(i).Description Debug.Print ThisWorkbook.VBProject.References.Item(i).GUID Debug.Print ThisWorkbook.VBProject.References.Item(i).Major Debug.Print ThisWorkbook.VBProject.References.Item(i).Minor Debug.Print ThisWorkbook.VBProject.References.Item(i).FullPath Next End Sub
2.2 Outlook中的默认文件夹
2.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
2.3 Outlook的标准项目
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)
2.3.1 标准项目清单:
Sub ol_6() With CreateObject("Outlook.Application") .CreateItem(0-7) End With End Sub
OlItemType |
value |
olAppointmentItem | 1 |
olContactItem | 2 |
olDistributionListItem | 7 |
olJournalItem | 4 |
olMailItem | 0 |
olNoteItem | 5 |
olPostItem | 6 |
olTaskItem | 3 |
3 Outlook中的VBA命令
Email属性