2012-09-05 83 views
2

我需要一個規則(或最有可能的VBA宏)來排序我的郵件。如果我在新接收的郵件的附件的文件名中每次都說「REPORT」,而不是我想將該郵件移動到另一個文件夾,那麼請說「REPORTS」文件夾。如何根據附件文件名將郵件移動到文件夾?

我該如何做到這一點?

我已經在郵件標題上設置了一條規則,但似乎沒有解決問題。

在此先感謝!

+0

其實我不是在VBA專家說真的,我還沒有嘗試過任何事情。 – modzsi

回答

0

http://www.outlookcode.com/article.aspx?id=62使用的代碼和http://blog.saieva.com/2010/03/27/move-messages-to-folders-with-outlook-vba/

'code goes in "ThisOutlookSession" module 
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String) 
    Dim arr() As String 
    Dim i As Integer 
    Dim ns As Outlook.NameSpace 
    Dim itm As MailItem 
    Dim m As Outlook.MailItem 
    Dim att As Outlook.Attachment 

    On Error Resume Next 
    Set ns = Application.Session 
    arr = Split(EntryIDCollection, ",") 
    For i = 0 To UBound(arr) 
     Set itm = ns.GetItemFromID(arr(i)) 
     If itm.Class = olMail Then 
      Set m = itm 
      If m.Attachments.Count > 0 Then 
       For Each att In m.Attachments 
        If UCase(att.FileName) Like "*REPORT*" Then 
         MoveToFolder m, "MoveTest" 
         Exit For 
        End If 
       Next att 
      End If 
     End If 
    Next 
    Set ns = Nothing 
    Set itm = Nothing 
    Set m = Nothing 
End Sub 


Sub MoveToFolder(mItem As MailItem, folderName) 

'###you need to edit this for your account name### 
Const mailboxNameString As String = "Mailbox - firstname lastname" 

Dim olApp As New Outlook.Application 
Dim olNameSpace As Outlook.NameSpace 
Dim olDestFolder As Outlook.MAPIFolder 

Set olNameSpace = olApp.GetNamespace("MAPI") 
Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(folderName) 

Debug.Print "[" & Date & " " & Time & "] " & _ 
       ": folder = " & folderName & _ 
       "; subject = " & mItem.Subject & "..." 

mItem.Move olDestFolder 

End Sub 
+0

謝謝蒂姆!它看起來不錯,我設法運行「MoveFolder」作爲一個單獨的模塊,但如果我只是窩在「ThisOutlookSession」沒有任何反應......任何猜測爲什麼? – modzsi

+0

當我測試時,我在ThisOutlookSession中有兩種方法,它工作正常。我不能說爲什麼它不適合你... –

+0

好吧,我會再看看,我以前使用Excel VB,但與Outlook的noob。無論如何,乾杯! – modzsi

相關問題