首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA Outlook Restrict返回"nothing“

VBA Outlook Restrict返回"nothing“
EN

Stack Overflow用户
提问于 2016-07-27 01:07:38
回答 2查看 122关注 0票数 0

这里还有一个与此类似的问题,我在那里实现了建议,但我仍然无法让它返回任何东西。非常简单;我每天早上6点01分在我的主收件箱中收到一封电子邮件,这就是这个脚本末尾的oOltargetEmail应该设置的值。我相信根据文档,.Restrict()中的字符串的格式是正确的,但是oOlItm从不带值。

代码语言:javascript
复制
        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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-27 03:37:26

在处理日期/时间属性时,请不要使用"=“-由于舍入错误,永远不会有精确的匹配。

票数 1
EN

Stack Overflow用户

发布于 2016-07-29 01:20:24

你们的两个建议都有助于解决这个问题。这就是最终有效的方法,以防将来有人偶然发现并无法弄清楚。主要问题是,由于@Dmitry Streblechenko提到的"=“运算符,ReceivedTime从未取值。谢谢你的帮助!

代码语言:javascript
复制
                           '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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38596010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档