将特定电子邮件从子文件夹移动到另一个子文件夹

时间:2016-06-23 22:41:55

标签: vba email outlook outlook-vba

也许我可以先说有类似的帖子讨论类似的问题,但我想使用与其他帖子中写的不同的代码,即Outlook VBA Macro to move mail from subfolder to subfolder

我使用的代码来自msdn网站(https://msdn.microsoft.com/en-us/library/office/ff869653.aspx),我想做的就是能够从收件箱内的子文件夹移动电子邮件,而不是使用收件箱到另一个子文件夹。

假设我要搜索邮件的子文件夹名称称为“营销”

我想修改的代码如下,

Sub CreateRule() 
Dim colRules As Outlook.Rules 
Dim oRule As Outlook.Rule 
Dim colRuleActions As Outlook.RuleActions 
Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction 
Dim oFromCondition As Outlook.ToOrFromRuleCondition 
Dim oExceptSubject As Outlook.TextRuleCondition 
Dim oInbox As Outlook.Folder 
Dim oMoveTarget As Outlook.Folder 

'Specify target folder for rule move action 
Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox) 
'Assume that target folder already exists 
Set oMoveTarget = oInbox.Folders("Advertisement") 

'Get Rules from Session.DefaultStore object 
Set colRules = Application.Session.DefaultStore.GetRules() 

'Create the rule by adding a Receive Rule to Rules collection 
Set oRule = colRules.Create("Advert rule", olRuleReceive) 

'Specify the condition in a ToOrFromRuleCondition object 
'Condition is if the message is from "xyz@abc.com" 
Set oFromCondition = oRule.Conditions.From 
With oFromCondition 
    .Enabled = True 
    .Recipients.Add ("xyz@abc.com") 
    .Recipients.ResolveAll 
End With 

'Specify the action in a MoveOrCopyRuleAction object 
'Action is to move the message to the target folder 
Set oMoveRuleAction = oRule.Actions.MoveToFolder 
With oMoveRuleAction 
    .Enabled = True 
    .Folder = oMoveTarget 
End With 

'Specify the exception condition for the subject in a TextRuleCondition object 
'Exception condition is if the subject contains "new" or "interest" 
Set oExceptSubject = _ 
    oRule.Exceptions.Subject 
With oExceptSubject 
    .Enabled = True 
    .Text = Array("new", "interest") 
End With 

'Update the server and display progress dialog 
colRules.Save 
End Sub 

我猜我需要修改这些行,但不确定它是如何在子文件夹而不是收件箱中搜索的。

'Specify target folder for rule move action 
Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

设置子文件夹

null

答案 1 :(得分:0)

Folder类提供Folders属性,该属性返回Folders集合,该集合表示指定Folder中包含的所有文件夹。

如果你需要一个名为" Marketing"的文件夹。您可以使用以下代码:

 Set oMoveTarget = oInbox.Folders("Marketing") 

如果文件夹位于树下,则必须递归调用Folders属性,例如,请参阅How to: Enumerate Folders

最后,您可能会发现Getting Started with VBA in Outlook 2010文章很有帮助。