Outlook .Restrict方法不适用于Date

时间:2014-12-10 07:25:06

标签: vba date outlook outlook-vba outlook-filter

Restrict()在外部指定时似乎不接受日期值。

Public Sub EBS()
    Dim oMail As MailItem
    Dim sPath As String
    Dim dtDate As Date
    Dim dtRecDate As Date
    Dim sName As String

    Dim oNameSpace As Outlook.NameSpace
    Dim oInboxFolder As Outlook.Folder
    Dim oSentFolder As Outlook.Folder
    Dim i As Long

    Set oNameSpace = Application.GetNamespace("MAPI")
    Set oInboxFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
    Set oSentFolder = oNameSpace.GetDefaultFolder(olFolderSentMail)

    dtRecDate = DateAdd("d", -180, Now)

    Set setItems = oInboxFolder.Items
    Set RestrictedItems = setItems.Restrict("[ReceivedTime] < dtRecDate AND [MessageClass] = 'IPM.Note'")
    For i = RestrictedItems.Count To 1 Step -1
        Set oMail = RestrictedItems.item(i)
        sName = oMail.Subject
        dtDate = oMail.ReceivedTime
        sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "_hhnnss", vbUseSystemDayOfWeek, vbUseSystem) & "_" & sName & ".msg"
        sName = Left(sName, 256)
        sPath = "C:\ARCHIVE\OUTLOOK\Inbox\"
        Debug.Print dtRecDate
        oMail.SaveAs sPath & sName, olMSG
        oMail.Delete
    Next i
End Sub

该限制适用于例如&#39; 2014/06/13&#39;用来代替dtRecDate

使用dtRecDate时,它不会限制任何项目。

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

我在代码中看到以下过滤条件:

"[ReceivedTime] < dtRecDate AND [MessageClass] = 'IPM.Note'"

您无法在字符串中声明对象。它不会自动转换为字符串。您必须在代码中执行此操作,例如:

"[ReceivedTime] < '" + Format(Date, "yyyy/mm/dd") +"' AND [MessageClass] = 'IPM.Note'"

Restrict方法在MSDN的页面上有以下示例:

sFilter = "[LastModificationTime] > '" & Format("1/15/99 3:30pm", "ddddd h:nn AMPM") & "'"