【问题标题】:How to move an email after assigning a category to another pst file folder?将类别分配到另一个 pst 文件夹后如何移动电子邮件?
【发布时间】:2022-08-17 22:55:11
【问题描述】:

我有一个老板,他收到大量电子邮件并为它们分配类别。在为它们分配类别后,我需要将这些电子邮件移至名为该类别的不同 pst 文件收件箱。我不需要自动创建收件箱。

我的代码将电子邮件移动到分配类别的默认收件箱文件夹中的文件夹。我需要它移动到另一个 pst 文件收件箱。

此人正在使用 POP3。我知道 IMAP 会更好,但由于老员工“意外”删除了电子邮件,它们以自己的方式设置。

Private WithEvents objInboxFolder As Outlook.Folder
Private WithEvents objInboxItems As Outlook.Items

\'Process inbox mails
Private Sub Application_Startup()
    Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set objInboxItems = objInboxFolder.Items
End Sub

\'Occurs when changing item
Private Sub objInboxItems_ItemChange(ByVal Item As Object)
    Dim objMail As Outlook.MailItem
    Dim objTargetFolder As Outlook.Folder
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
 
       \'Move mails based on color category
       If InStr(objMail.Categories, \"Followup\") > 0 Then
          Set objTargetFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders(\"Followup\")
          objMail.Move objTargetFolder
       ElseIf InStr(objMail.Categories, \"Business\") > 0 Then
          Set objTargetFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders(\"Business\")
          objMail.Move objTargetFolder
       End If
    End If
End Sub

标签: vba outlook


【解决方案1】:

在将这些电子邮件分配给类别后,我需要将这些电子邮件移至与类别名称相同的不同 pst 文件收件箱。

首先,应将其他 pst 文件添加到配置文件中。否则,如果 pst 文件存储在磁盘上,则需要使用 AddStore/AddStoreEx 方法添加它们。

如果其他存储只是共享文件夹,则需要使用 NameSpace.GetSharedDefaultFolder 方法,该方法返回一个 Folder 对象,该对象代表指定用户的指定默认文件夹。此方法用于委派方案,其中一个用户已将一个或多个默认文件夹(例如,他们的共享收件箱文件夹)的访问权限委派给另一个用户。

Sub ResolveName()
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Dan Wilson") 
 myRecipient.Resolve 
 If myRecipient.Resolved Then 
   Call ShowCalendar(myNamespace, myRecipient) 
 End If 
End Sub 
 
Sub ShowCalendar(myNamespace, myRecipient) 
 Dim CalendarFolder As Outlook.Folder 
 
 Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar) 
 CalendarFolder.Display 
End Sub

最后,当商店可用时,您可以使用Namespace 类的Stores 属性,该属性返回代表当前配置文件中所有Store 对象的Stores 集合对象。

Sub EnumerateStores() 
 Dim colStores As Outlook.Stores
 Dim oStore As Outlook.Store 
 Dim oRoot As Outlook.Folder 
 
 On Error Resume Next 
 Set colStores = Application.Session.Stores 
 For Each oStore In colStores 
   Set oRoot = oStore.GetRootFolder 
   Debug.Print (oRoot.FolderPath) 
   Debug.Print (oRoot.Name) 
 Next 
End Sub 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多