2016-06-23 99 views
-1

也許我可以說,有一個類似的帖子在討論類似的問題開始,但我想用不同的代碼比寫在其他職位即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 "[email protected]" 
Set oFromCondition = oRule.Conditions.From 
With oFromCondition 
    .Enabled = True 
    .Recipients.Add ("[email protected]") 
    .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) 

您的幫助將不勝感激!

+0

從'Marketing'到'Where'?所有的電子郵件或特定的電子郵件? – 0m3r

+0

從市場營銷到廣告(設置oMoveTarget = oInbox.Folders(「廣告」)),並移除除主題中「新」或「有興趣」之外的所有電子郵件。代碼的工作原理是將郵件從「收件箱」移動到「廣告」 – Nosferatu

回答

0

設置你的子文件夾

'Specify target folder for rule move action 
Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Marketing") 
+1

哦哇...這就像一個魅力... 這正是我想要的!再次謝謝! – Nosferatu

0

的文件夾類提供Folders屬性返回Folders集合代表所有包含在指定文件夾中的文件夾。

如果你需要得到一個文件夾命名爲「市場營銷」,你可以使用下面的代碼:

Set oMoveTarget = oInbox.Folders("Marketing") 

在情況下,如果文件夾位於下的樹,你將不得不遞歸調用的文件夾屬性,例如,參見How to: Enumerate Folders

最後,您可能會發現Getting Started with VBA in Outlook 2010文章有幫助。

+0

感謝您的信息。看看鏈接,但我正在尋找的答案是添加以下內容,所以我可以從該子文件夾移動的東西。 – Nosferatu

相關問題