vba Outlook .Restrict 方法不适用于日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27395382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:28:31  来源:igfitidea点击:

Outlook .Restrict method does not work with Date

vbadateoutlookoutlook-vbaoutlook-filter

提问by clippertm

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

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

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

例如,当使用 '2014/06/13' 而不是dtRecDate.

When dtRecDateis used, it does not restrict any item.

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

Can you please help?

你能帮忙吗?

回答by Eugene Astafiev

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 Restrictmethod has the following sample on the page in MSDN:

限制方法在MSDN页面上下面的示例:

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