gpt4 book ai didi

vba - 如果存在旧的未读邮件,请发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 23:39:06 24 4
gpt4 key购买 nike

如果有超过 15 分钟的未读电子邮件,我会尝试向自己发送电子邮件。

代码,当我从 Outlook 中手动运行时,发送了邮件,但我收到了

Run time error '-2147221238' (8004010a)

可能由于上述错误,我无法让它从规则运行或独立于任务计划。

Sub checkForUnreadMails()

Dim objFolder, objNamespace
'get running outlook application or open outlook
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If

Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objMsg = Application.CreateItem(olMailItem)

strFilter = "[received] <= '" & Format(DateAdd("n", -15, Now()), "ddddd h:nn AMPM") & "'"
Debug.Print strFilter
Set inboxItems = objNamespace.GetDefaultFolder(olFolderInbox).Items.Restrict(strFilter)
strFilter = "[Unread] = True"
Set unreadItems = inboxItems.Restrict(strFilter)

For Each itm In unreadItems
With objMsg
.To = "email@email.com"
.Subject = "outlookrule There are unread emails over 15 minutes old in Vision ATM mailbox"
.Categories = "T"
.BodyFormat = olFormatPlain ' send plain text message
.Importance = olImportanceHigh
.Sensitivity = olConfidential
.Send
End With
Next
End Sub

最佳答案

错误代码是 MAPI_E_OBJECT_DELETED。您的代码没有多大意义 - 您创建了 objMsg 一次,但尝试为每个未读项目多次发送它(您不能这样做)。

为什么您要为每封未读电子邮件发送多次电子邮件?您实际上并没有从该电子邮件中检索任何信息。要么简单地检查是否有匹配的电子邮件 (unreadItems.Count > 0) 并发送一次电子邮件,要么创建一条新消息 (Set objMsg = Application.CreateItem(olMailItem) ) 在循环的每次迭代中包含一些特定的电子邮件详细信息。

Sub checkForUnreadMails()

Dim objFolder, objNamespace
'get running outlook application or open outlook
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If

Set objNamespace = objOutlook.GetNamespace("MAPI")

strFilter = "[received] <= '" & Format(DateAdd("n", -15, Now()), "ddddd h:nn AMPM") & "'"
Debug.Print strFilter
Set inboxItems = objNamespace.GetDefaultFolder(olFolderInbox).Items.Restrict(strFilter)
strFilter = "[Unread] = True"
Set unreadItems = inboxItems.Restrict(strFilter)
if unreadItems.Count > 0 Then
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "email@email.com"
.Subject = "outlookrule There are unread emails over 15 minutes old in Vision ATM mailbox"
.Categories = "T"
.BodyFormat = olFormatPlain ' send plain text message
.Importance = olImportanceHigh
.Sensitivity = olConfidential
.Send
End With
End If
End Sub

关于vba - 如果存在旧的未读邮件,请发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028017/

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