zoukankan      html  css  js  c++  java
  • Outlook API

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

  • 相关阅读:
    Windows Azure Cloud Service (5) 由过渡环境向生产环境过渡
    rpcss.dll丢失造成任务栏不见
    css文本省略号
    字符串是否包含中文?
    在 System.NullReferenceException 中第一次偶然出现的“ComServer.exe”类型的异常
    取参数的正则表达式
    EverNote死机的问题
    找尺子
    读书笔记
    水晶按钮的学习
  • 原文地址:https://www.cnblogs.com/yl153/p/6711519.html
Copyright © 2011-2022 走看看