Outlook .Restrict method does not work with Date

試著忘記壹切 提交于 2019-12-11 12:01:51

问题


Restrict() does not seem to accept a date value when it is specified outside.

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

The restriction works when, for example, '2014/06/13' is used instead of dtRecDate.

When dtRecDate is used, it does not restrict any item.

Can you please help?


回答1:


I see the following filter criteria in the code:

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

You can't declare object in the string. It will not be converted to string automatically. You have to do so in the code, for example:

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

The Restrict method has the following sample on the page in MSDN:

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


来源:https://stackoverflow.com/questions/27395382/outlook-restrict-method-does-not-work-with-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!