这里有另外一个类似的问题,我在那里实施了建议,但我仍然无法让它返回任何东西。非常简单;我每天06:01在主收件箱收到一封电子邮件,这就是oOltargetEmail应该在这个脚本的末尾设置的内容。我相信.Restrict()中的字符串是基于文档正确格式化的,但oOlItm永远不会取值。
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 :(得分:1)
永远不要使用" ="使用日期/时间属性时 - 由于舍入错误,永远不会有完全匹配。
答案 1 :(得分: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