2017-08-17 19 views
1

我有以下代碼對收到的每個通過Outlook收到的電子郵件執行一些操作。它可以工作,但是如果多個郵件同時到達(即,當Outlook重新查詢服務器時,我的電子郵件地址是基於),它只會在最近收到的代碼上運行下面的代碼。有什麼建議麼?掃描所有傳入的電子郵件Outlook

Private WithEvents Items As Outlook.Items 

Private Sub Application_Startup() 
    Dim olApp As Outlook.Application 
    Dim objNS As Outlook.NameSpace 
    Set olApp = Outlook.Application 
    Set objNS = olApp.GetNamespace("MAPI") 
    ' default local Inbox 
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub 

Sub Items_ItemAdd(ByVal item As Object) 
    On Error GoTo ErrorHandler 
    Dim Msg As Outlook.MailItem 
    If TypeName(item) = "MailItem" Then 
    Set Msg = item 
    If InStr(Msg.SentOnBehalfOfName, "name") <> 0 Then 
     'Do Something 
    End If 
    End If 
ProgramExit: 
    Exit Sub 
ErrorHandler: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume ProgramExit 
End Sub 

回答

0

您可以在文件夾中的項目上運行代碼。

Sub Items_ItemAdd(ByVal item As Object) 
    On Error GoTo ErrorHandler 
    Dim Msg As Outlook.MailItem 
    If TypeName(item) = "MailItem" Then 
     Set Msg = item 
     If InStr(Msg.SentOnBehalfOfName, "name") <> 0 Then 
      'Do Something 
      ' Move Msg to a "Done" folder 
      ' or mark it read or some way 
      ' you can use to not reprocess an item 
     End If 
    End If 

    SkippedItems 

ProgramExit: 
    Exit Sub 

ErrorHandler: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume ProgramExit 
End Sub 


Sub SkippedItems 

    dim i as long 
    Dim skippedMsg As MailItem 
    dim inboxItems as items 
    dim inboxItemsCount as long 

    On Error GoTo ErrorHandlerSkippedItems 
    set inboxItems = session.GetDefaultFolder(olFolderInbox).Items 
    inboxItemsCount = inboxItems.count 

    if inboxItemsCount > 0 then 

     for i = inboxItemsCount to 1 step -1 

      If TypeName(inboxItems(i)) = "MailItem" Then 
       Set skippedMsg = inboxItems(i) 
       If InStr(skippedMsg.SentOnBehalfOfName, "name") <> 0 Then 
        'Do Something 
        ' Move SkippedMsg to a "Done" folder 
        ' or mark it read or some way 
        ' you can use to not reprocess an item 

        set skippedMsg = nothing 
       End If 
      End If 
     Next 

    End If 

ProgramExitSkippedItems: 
    set skippedMsg = nothing 
    set inboxItems = nothing 
    Exit Sub 

ErrorHandlerSkippedItems: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume ProgramExitSkippedItems 
End Sub 
+0

由於時間的原因,我寧願不這樣做。 –

+0

您可以將代碼轉換爲從規則運行或轉換爲newMailEx代碼,並查看是否更好。 – niton

相關問題