1、Outlook简介

若要从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对象的新实例,使用Set语句将对象变量设置为Application对象的新实例。也可以在Dim、Private、Public 或 Static语句中使用New关键字来声明对象变量。Application对象的新实例在第一次引用该变量时创建。

若要启动Outlook自动化会话,可以使用前期绑定或后期绑定。后期绑定使用GetObject或CreateObject函数初始化Outlook。例如,以下代码将对象变量设置为Outlook Application对象,该对象为Outlook对象模型中的最高层对象。所有自动化代码都必须首先定义Outlook Application对象,才能够访问其他Outlook对象。

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,如下所示:

Set ol = New Outlook.Application
Set ns = ol.GetNameSpace("MAPI")
Set f = ns.GetDefaultFolder(olFolderContacts)

2、访问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().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().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().Items.Count
Debug.Print "3InBox中邮件的总数为:"; counter End Sub

 2.1.4  获取动态引用:

Sub GetRefInfo()
Dim i As Integer On Error Resume Next
For i = 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().Name '删除的项目Deleted items
Debug.Print .GetDefaultFolder().Name '发件箱PostOut
Debug.Print .GetDefaultFolder().Name '发送项目Sent items
Debug.Print .GetDefaultFolder().Name '收件箱PostIn
Debug.Print .GetDefaultFolder().Name '日历Canlender
Debug.Print .GetDefaultFolder().Name '联系人Contacts
Debug.Print .GetDefaultFolder().Name '日记Journals
Debug.Print .GetDefaultFolder().Name '便签Notes
Debug.Print .GetDefaultFolder().Name '任务Tasks
Debug.Print .GetDefaultFolder().Name '提醒Reminders
Debug.Print .GetDefaultFolder().Name '提醒Reminders
Debug.Print .GetDefaultFolder().Name '草稿Drafts
End With End Sub
OlDefaultFolders常量 Value
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

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(-)
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属性

最新文章

  1. 20170103简单解析MySQL查询优化器工作原理
  2. CF720F
  3. iOS中dyld缓存的实现原理是怎样的?
  4. .NET 程序在 Windows XP 下调用 SHA512CryptoServiceProvider 方法报 PlatformNotSupportedException 异常
  5. ecshop session机制
  6. leetcode:Rotate Array
  7. (原创)monitor H3C switch with cacti
  8. 配置ADB 工具 (Win7_64)
  9. magic_quotes_gpc、mysql_real_escape_string、addslashes的区别及用法
  10. NDN与TCP/IP
  11. android图像模糊技术
  12. Spring MVC 笔记 —— Spring MVC 文件上传
  13. asp.net mvc 4 项目升级到 asp.net mvc5
  14. fiddler实现手机抓包
  15. Docker进阶之二:Docker内部组件
  16. OpenStack-Nova(4)
  17. 4.17 小发现(dalao勿点)
  18. MongoDB的简单操作
  19. bazel build //tensorflow/examples/android:tensorflow_demo报错: fatal error: 'cuda_runtime.h' file not found
  20. js中input文本框设置和移除默认值

热门文章

  1. 模仿UIApplication创建单例
  2. FileProvider的使用
  3. [b0005] Linux 系统常用操作命令
  4. Python Pyinstaller 打包程序及遇到的问题总结
  5. yum update 执行报错: error : unpacking of archive failed on file /usr/.../...;5d26ff7c: cpio : symlink
  6. 第十二章 WEB渗透
  7. nginx 图片访问404 (使用location中使用 root,alias的区别)
  8. 密度峰值聚类算法MATLAB程序
  9. 攻防世界pwn-Mary_Morton
  10. Django 连接数据库