VBA Outlook event moving email

后端 未结 1 2044
攒了一身酷
攒了一身酷 2020-12-11 09:22

I search a way to get the event of a moving item/email in outlook.

Can we use an Inspector? Or maybe there\'s an event handler like itemsent or newmail?

Than

1条回答
  •  春和景丽
    2020-12-11 09:56

    An event is fired when an item is added to a collection, in a folder. For example, suppose you had a folder called "Stuff" one level below your default Inbox. This code would fire every time an email was moved to that folder:

    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
      Dim olApp As Outlook.Application
    
      Set olApp = Outlook.Application
      Set Items = GetNS(olApp).GetDefaultFolder(olFolderInbox).Folders("Stuff").Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal item As Object)
    
      On Error GoTo ErrorHandler
    
      MsgBox "You moved an item into the 'Stuff' folder."
    
    ProgramExit:
      Exit Sub
    ErrorHandler:
      MsgBox Err.Number & " - " & Err.Description
      Resume ProgramExit
    End Sub
    
    Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
      Set GetNS = app.GetNamespace("MAPI")
    End Function
    

    Paste this into ThisOutlookSession and restart Outlook. Whenever an email is moved to that folder you will see the popup.

    0 讨论(0)
提交回复
热议问题