VBA宏可将项目从Outlook中的所有文件夹移动到特定文件夹

时间:2018-11-09 13:33:58

标签: vba outlook

我正在尝试将在Outlook中分类的邮件移动到特定文件夹。我的问题是,它仅适用于“收件箱”文件夹,我想将其应用于所有子文件夹。这是我的下面的代码。

Public WithEvents objExplorer As Outlook.Explorer
Public WithEvents objMail As Outlook.MailItem

Private Sub Application_Startup()
    Set objExplorer = Outlook.Application.ActiveExplorer
End Sub

Private Sub objExplorer_SelectionChange()
    On Error Resume Next
    Set objMail = objExplorer.Selection.Item(1)
End Sub

Private Sub objMail_Forward(ByVal Response As Object, Cancel As Boolean)
    Dim objInboxFolder As Folder
    Dim objTargetFolder As Folder

    Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)

    On Error Resume Next
    Set objTargetFolder = objInboxFolder.parent.Folders("Forwarded")
    If objTargetFolder Is Nothing Then
       Set objTargetFolder = objInboxFolder.parent.Folders.Add("Forwarded")
    End If
    objMail.Categories = "Forwarded"
    objMail.Move objTargetFolder
End Sub

我知道我可以通过执行以下操作将其应用于另一个文件夹,例如“ interface”。

Set objFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("Interface")

但是,如果我有30个以上的文件夹,这将很复杂,是否可以将我的代码应用于Outlook中的所有文件夹?

0 个答案:

没有答案