Vba 按邮件列表上的ReceivedTime设置限制

Vba 按邮件列表上的ReceivedTime设置限制,vba,outlook,Vba,Outlook,我试图阅读今天才收到的邮件。下面是我限制但其抛出条件无效错误的代码。当我给出一个类似于unread=True的条件时,同样可以正常工作 Set myItems = myItems.Restrict("DateValue[ReceivedTime]='" & Format(DateValue(Now),"ddddd h:nn AMPM") & "'") 请在这方面帮助我。我发现至少有两个问题 您有“DateValue[ReceivedTime]”而不是“[ReceivedTime

我试图阅读今天才收到的邮件。下面是我限制但其抛出条件无效错误的代码。当我给出一个类似于
unread=True
的条件时,同样可以正常工作

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

请在这方面帮助我。

我发现至少有两个问题

  • 您有“DateValue[ReceivedTime]”而不是“[ReceivedTime]”
  • 您将电子邮件限制为今天午夜收到的邮件,而不是午夜之后收到的邮件 请尝试以下代码:

    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
    

    谢谢你的回复,托尼。它确实帮助“将FmtToday设置为字符串FmtToday=Format(DateValue(Now()),“ddddd h:nn AMPM”)设置mailitestoday=FldrInbox.Items.Restrict(“[ReceivedTime]>””&FmtToday&“”)。我已经用这个修改了我的代码,它成功了。