【发布时间】:2020-02-27 13:56:06
【问题描述】:
当我为其分配类别时,我想将电子邮件移动到收件箱的子文件夹中
我从Extended Office 找到了以下代码,但它不起作用。 它应该将邮件移动到与类别同名的子文件夹,如果不存在则创建一个文件夹。
我在 Outlook 的安全设置中启用了宏,并插入了一些消息框警报以确认确实运行。
代码在 ThisOutlookSession 中
Private WithEvents xInboxFld As Outlook.Folder
Private WithEvents xInboxItems As Outlook.Items
Private Sub Application_Startup()
MsgBox "Macros are working"
Set xInboxFld = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
Set xInboxItems = xInboxFld.Items
End Sub
Private Sub xInboxItems_ItemChange(ByVal Item As Object)
MsgBox "Item Changed"
Dim xMailItem As Outlook.MailItem
Dim xFlds As Outlook.Folders
Dim xFld As Outlook.Folder
Dim xTargetFld As Outlook.Folder
Dim xFlag As Boolean
On Error Resume Next
If Item.Class = olMail Then
Set xMailItem = Item
xFlag = False
If xMailItem.Categories <> "" Then
Set xFlds = Application.Session.GetDefaultFolder(olFolderInbox).Folders
If xFlds.Count <> 0 Then
For Each xFld In xFlds
If xFld.Name = xMailItem.Categories Then
xFlag = True
End If
Next
End If
If xFlag = False Then
Application.Session.GetDefaultFolder(olFolderInbox).Folders.Add xMailItem.Categories, olFolderInbox
End If
Set xTargetFld = Application.Session.GetDefaultFolder(olFolderInbox).Folders(xMailItem.Categories)
xMailItem.Move xTargetFld
End If
End If
End Sub
【问题讨论】:
-
On Error Resume Next只是隐藏错误。删除它 - 您是否收到错误消息以及在哪一行? -
为什么不为分配到特定类别的时间设置规则?
-
一个规则不起作用,因为它只在邮件进来时运行,而不是在它实际分配到一个类别时运行。