【发布时间】:2022-07-09 20:43:33
【问题描述】:
我有 VBA 代码,可以将选定的邮件标记为已读,分配一个类别并将它们移动到子文件夹。
邮件递送系统“无法递送”报告未标记为已读、分类或移动。
我尝试复制 For Each 循环以查找 olReportItem。 (我意识到有两个循环是低效的,但我这样做只是为了测试目的,所以我可以将所有 beta 代码放在一个部分中。)
Sub TestMoveToSubfolder()
'With selected emails: (1) mark as read, (2) assign category, (3) move to subfolder
On Error Resume Next
Dim thisFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Dim objStore As Store
Set thisFolder = Application.ActiveExplorer.CurrentFolder
Set objStore = thisFolder.Store
Set objFolder = thisFolder.Folders("REFERENCE_DESIRED_FOLDER")
'Be sure target folder exists
If objFolder Is Nothing Then
MsgBox "I can't find the designated subfolder.", vbOKOnly + vbExclamation, "INVALID SUBFOLDER"
Exit Sub
End If
'Confirm at least one message is selected
If Application.ActiveExplorer.Selection.Count = 0 Then
Exit Sub
End If
'Loop through emails
For Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.UnRead = False
objItem.Categories = "INSERT_DESIRED_CATEGORY"
objItem.Move objFolder
End If
End If
Next
'TEST SECTION to work with undeliverable reports
Dim objItem2 As Outlook.ReportItem
'Loop through nondelivery reports
For Each objItem2 In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem2.Class = olReportItem Then
objItem2.UnRead = False
objItem2.Categories = "INSERT_DESIRED_CATEGORY"
objItem2.Move objFolder
End If
End If
Next
Set objItem2 = Nothing
Set thisFolder = Nothing
Set objFolder = Nothing
Set objItem = Nothing
Set objStore = Nothing
End Sub
【问题讨论】:
-
On Error Resume Next大约有 99.9999999% 的时间被滥用,因此您并不孤单。删除它以查看错误。在网站上搜索解决方案。 -
olReportItem类不存在,恐怕。请改用olReport...并评论On Error Resume Next,因为(正确)已在上述评论中说明。objItem应声明为As Variant或As Object。