这里还有一个与此类似的问题,我在那里实现了建议,但我仍然无法让它返回任何东西。非常简单;我每天早上6点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发布于 2016-07-27 03:37:26
在处理日期/时间属性时,请不要使用"=“-由于舍入错误,永远不会有精确的匹配。
发布于 2016-07-29 01:20:24
你们的两个建议都有助于解决这个问题。这就是最终有效的方法,以防将来有人偶然发现并无法弄清楚。主要问题是,由于@Dmitry Streblechenko提到的"=“运算符,ReceivedTime从未取值。谢谢你的帮助!
'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)
Nexthttps://stackoverflow.com/questions/38596010
复制相似问题