還有一個類似於這個問題的其他問題,並且我在那裏實現了這些建議,但是我仍然無法獲得此返回的任何內容。很簡單;每天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