gpt4 book ai didi

excel - 如何根据接收时间对邮件应用过滤器?

转载 作者:行者123 更新时间:2023-12-04 21:32:51 27 4
gpt4 key购买 nike

我正在使用以下 For Each 循环将新的 Outlook 邮件导入 Excel。我正在尝试加快例程的速度,但无法将此过滤器应用于我现有的代码。

myInbox.Items.Restricted("DateValue[ReceivedTime] > "" & Format(DateValue(Now, "ddddd h:nn AMPM") & "")

有人可以提供一些指示吗?提前致谢!
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.Items
Dim myitems As Outlook.Items
Dim myitem As Object
Dim Found As Boolean

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.Session.Folders.Item("test@test.com").Folders("My Folder").Items
' Need to apply filter to only pull items new than a specified date'
Found = False
For Each myitem In myInbox
'This next line is checking all items in folder which is taking too long'
If myitem.Class = olMail And myitem.ReceivedTime > Workbooks("Test.xlsm").Sheets("Sheet1").Range("C2").Value Then
'Do something'
Found = True
End If
Next myitem
Set myOlApp = Nothing

最佳答案

为什么对 DateValue 如此着迷?

这是错误的Format(DateValue(Now, "ddddd h:nn AMPM")这是"DateValue[ReceivedTime]
即使您更正了该语法,您也会尝试设置大于当前时间的过滤器,因此您正在尝试限制尚未收到的项目:)

使用变量存储某个时间(即早于当前日期时间)

以下代码过滤 2017 年 11 月 4 日晚上 10:25:00 之后收到的所有项目

Sub test()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Items
Dim myItems As Outlook.Items
Dim myitem As Object
Dim Found As Boolean
Dim TimeCrit As Date

TimeCrit = #11/4/2017 10:25:00 PM#

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Items
' Need to apply filter to only pull items new than a specified date'
Set myItems = myInbox.Restrict("[ReceivedTime] >= """ & Format(TimeCrit, "ddddd h:nn AMPM") & """")

Found = False
For Each myitem In myItems
MsgBox myitem.Subject
Next myitem
Set myOlApp = Nothing


End Sub

关于excel - 如何根据接收时间对邮件应用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128442/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com