2016-07-26 91 views
0

还有一个类似于这个问题的其他问题,并且我在那里实现了这些建议,但是我仍然无法获得此返回的任何内容。很简单;每天06:01,我的主收件箱中会收到一封电子邮件,这是在脚本结尾处应该设置的oOltargetEmail。我相信.Restrict()中的字符串基于文档格式正确,但oOlItm永远不会有任何价值。VBA Outlook Restrict返回“nothing”

 Const olFolderInbox As Integer = 6 
     Const AttachmentPath As String = "C:\Users\TRBYE\Desktop\cheeseReport.csv" 

     Private Sub CommandButton1_Click() 

     Dim oOlAp As Object, oOlns As Object, oOlInb As Object, oOlItm As Object, oOltargetEmail As Object, oOlAtch As Object 
     Dim beginningDate As String, endingDate As String, todaysDateTime As String, todaysDate As String, receivedTime As String, date1 As String 
     Dim x As Integer 

     Set oOlAp = GetObject(, "Outlook.application") 
     Set oOlns = oOlAp.GetNamespace("MAPI") 
     Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox) 

     receivedTime = " 06:01 AM" 
     todaysDateTime = Format(Now(), "ddddd hh:mm AMPM") 
     x = Len(todaysDateTime) 
     todaysDate = Left(todaysDateTime, (Len(todaysDateTime) - 9)) 

     'set start and end time based on strings from above' 
     beginningDate = todaysDate & receivedTime 

     'determine corrrect email' 
     For Each oOlItm In oOlInb.Items.Restrict("[ReceivedTime] = '" & Format(beginningDate, "ddddd h:nn AMPM") & "'") 
      Set oOltargetEmail = oOlItm 
     Next 

     'download attachment to desktop' 
     For Each oOlAtch In oOltargetEmail.Attachments 
      oOlAtch.SaveAsFile AttachmentPath 
     Next 

     'open attachment' 
     Workbooks.Open (AttachmentPath) 
     End Sub 

回答

0

使用日期/时间属性时不要使用“=” - 由于舍入误差,永远不会有完全匹配。

0

你的两个建议帮助我们弄清楚了这一点。这是结束了工作,以防万一未来有人绊倒了这一点,并无法弄清楚。最主要的问题是[ReceivedTime]从来没有像@Dmitry Streblechenko提到的那样使用“=”运算符。谢谢您的帮助!

      'determine corrrect email' 
          For Each oOlItm In oOlInb.Items.Restrict("[ReceivedTime] > '" & Format(beginningDate, "ddddd h:nn AMPM") & "' And [ReceivedTime] < '" & Format(endingDate, "ddddd h:nn AMPM") & "'") 
           Set oOltargetEmail = oOlItm 

            'download attachment to desktop' 
            For Each oOlAtch In oOltargetEmail.Attachments 
             oOlAtch.SaveAsFile AttachmentPath 
            Next 

            'open attachment' 
            Workbooks.Open(AttachmentPath) 
          Next