2016-07-26 93 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