【VBS】检索Outlook本地邮箱

实现功能:使用VBS检索Outlook本地邮箱中,今天是否收到某标题的邮件。

代码如下:

Const C_DATE_FORMAT_MAIL_FILTER_FROM = 1        ' yyyy-m-d 0:00 AM
Const C_DATE_FORMAT_MAIL_FILTER_TO = 2          ' yyyy-m-d 11:59 PM

Function CheckMail(pMailFolder, pMailTitle)
    On Error Resume Next
    Dim objOlApp
    Dim objNameSpace
    Dim objFolder
    Dim strFilter

    CheckMail = False
    Set objOlApp = CreateObject("Outlook.Application")
    Set objNameSpace = objOlApp.GetNamespace("MAPI")
    'olFolderInbox  6  默认收件箱
    Set objFolder = objNameSpace.GetDefaultFolder(6).Folders(pMailFolder)

    If Err.Number <> 0 Then
        AddLog ("邮件访问出错:" & Err.Description)
        WScript.Quit -1
    End If

    '过滤条件
    strFilter = "[Subject] = '" & pMailTitle _
        & "' And [ReceivedTime] >= '" & FormatDate(Now(), C_DATE_FORMAT_MAIL_FILTER_FROM) _
        & "' And [ReceivedTime] <= '" & FormatDate(Now(), C_DATE_FORMAT_MAIL_FILTER_TO) & "'"

    '检索
    If objFolder.Items.Restrict(strFilter).Count > 0 Then
        CheckMail = True
    End If
End Function

'日期格式化
Function FormatDate(pDate, pFlag)
    Dim y, m, d
    FormatDate = ""
    If IsDate(pDate) = False Then Exit Function
    y = cstr(year(pDate))
    m = cstr(month(pDate))
    d = cstr(day(pDate))

    Select Case pFlag
        Case C_DATE_FORMAT_MAIL_FILTER_FROM
            ' yyyy-m-d 0:00 AM From
            FormatDate = y & "-" & m & "-" & d & " 0:00 AM"
        Case C_DATE_FORMAT_MAIL_FILTER_TO
            ' yyyy-m-d 11:59 PM To
            FormatDate = y & "-" & m & "-" & d & " 11:59 PM"
    End Select
End Function

 

你可能感兴趣的:(out)