【问题标题】:Outlook VBA - move mail when assigned to a categoryOutlook VBA - 分配给类别时移动邮件
【发布时间】: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 只是隐藏错误。删除它 - 您是否收到错误消息以及在哪一行?
  • 为什么不为分配到特定类别的时间设置规则?
  • 一个规则不起作用,因为它只在邮件进来时运行,而不是在它实际分配到一个类别时运行。

标签: vba outlook


【解决方案1】:

我不知道确切原因,但是今天突然开始工作,我之前曾多次重新启动 Outlook,但今天早上我需要强制关闭 Outlook 之后它开始工作。 (我什至不确定它是否因为重新启动而立即开始工作,或者它是否是由其他东西触发的很短的时间)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2011-12-22
    • 2016-05-15
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多