outlook - Outlook 自动化移动电子邮件 - 运行时错误 13
问题描述
我想自动化我的 MS Outlook 收件箱。这个想法是将所有电子邮件(i)具有特定发件人地址和(ii)从今天开始超过 7 天到我的收件箱的子文件夹中。请参阅下面的工作示例(您可能需要调整文件夹名称以便它在您的机器上工作)。
我的问题:经过 88 次迭代后,我遇到了“运行时错误 13,类型不匹配”。为什么经过这么多次迭代会发生这种情况?而且,更重要的是,如何解决它?有任何想法吗?
我的 VBE 上启用了所有默认库。我正在使用 MS Office 2019。
谢谢!
'On Error Resume Next
On Error GoTo 0
'-----------------------------------------------------------------------------------------
' declare variables
'-----------------------------------------------------------------------------------------
Dim objSourceFolder As MAPIFolder
Dim objDestinationFolder As MAPIFolder
Dim objMail As MailItem ' single email
Dim objMails As Items ' all emails in source folder
Dim lngItems As Long ' number of checked emails
Dim intDays As Integer ' number of days
Dim counter As Integer ' number of moved emails
'-----------------------------------------------------------------------------------------
' email age in days
'-----------------------------------------------------------------------------------------
intDays = 7
'-----------------------------------------------------------------------------------------
' define folder (= inbox)
'-----------------------------------------------------------------------------------------
Set objSourceFolder = GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
'-----------------------------------------------------------------------------------------
' reference items in source folder
'-----------------------------------------------------------------------------------------
Set objMails = objSourceFolder.Items
'objMails.Count
'-----------------------------------------------------------------------------------------
' sort emails in source folder (oldest first)
'-----------------------------------------------------------------------------------------
objMails.Sort "ReceivedTime", False
'-----------------------------------------------------------------------------------------
' move email
'-----------------------------------------------------------------------------------------
For Each objMail In objMails
If objMail.ReceivedTime < Now - intDays Then
Select Case objMail.SenderEmailAddress
Case "mailrobot@mail.xing.com":
Set objDestinationFolder = GetNamespace("Mapi").Folders(1).Folders("Inbox").Folders("Xing")
End Select
If objDestinationFolder Is Nothing Then
Else: objMail.Move objDestinationFolder
counter = counter + 1
End If
lngItems = lngItems + 1
End If
Next
End Sub
解决方案
您的代码假定您只能MailItem
在收件箱文件夹中有对象。你也有ReportItem
和MeetingItem
对象。
声明objMail
为通用对象并在循环中首先检查Class
属性是否为 43 ( OlObjectClass.olMail
)
推荐阅读
- amazon-web-services - 如何避免单页应用程序中的 CORS 预检请求?
- python-3.x - PySpark UDF 适用于样本集,但不适用于大数据
- javascript - 如何使用提供的参数限制对范围的 ajax 调用?
- c++ - 如何重载语句以将字符串与类对象连接起来?
- r - 根据复杂模式列出文件
- swift - 导航栏不可见 swift
- javascript - 如何解决 Laravel 6 中的“当前未启用对实验语法 'classProperties' 的支持”?
- sql - SQL Server 连接查询 - 需要帮助
- haskell - 当我尝试混合地板、除法和加法时,为什么 Haskell 会抱怨?
- python - 在 XML 中插入父元素不会保留其父元素(Python)