use*_*962 2 outlook vba runtime-error outlook-vba
如果有超过15分钟的未读电子邮件,我正在尝试向自己发送电子邮件。
当我从Outlook内部手动运行时,该代码发送了邮件,但我收到了
运行时错误'-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
Run Code Online (Sandbox Code Playgroud)
错误代码为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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |