在邮件列表上设置ReceivedTime的限制

时间:2017-07-04 06:05:56

标签: outlook-vba

我试图阅读仅在今天收到的邮件。下面是限制但是它抛出条件无效错误的代码。当我给出像unread = True这样的条件时,同样的工作正常。

Set myItems = myItems.Restrict("DateValue[ReceivedTime]='" & Format(DateValue(Now),"ddddd h:nn AMPM") & "'")

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我至少看到两个问题。

  1. 您有“DateValue [ReceivedTime]”而不是“[ReceivedTime]”。
  2. 您正在限制今天午夜收到的电子邮件,而不是午夜之后收到的电子邮件。
  3. 试试这段代码:

    Sub RestrictByDate()
    
      Dim FmtToday As String
      Dim FldrInbox As Folder
      Dim MailItemsToday As Items
      Dim MailItemCrnt As MailItem
    
      FmtToday = Format(DateValue(Now()), "ddddd h:nn AMPM")
    
      ' #### Replace "xxxx" with the name of the store containing the target Inbox
      Set FldrInbox = Session.Folders("xxxx").Folders("Inbox")
    
      Set MailItemsToday = FldrInbox.Items.Restrict("[ReceivedTime] > '" & FmtToday & "'")
    
      Debug.Print "Number of emails received today=" & MailItemsToday.Count
      For Each MailItemCrnt In MailItemsToday
        With MailItemCrnt
          Debug.Print .ReceivedTime & " " & .Subject
        End With
      Next
    
    End Sub