VBA Outlook event moving email

时光毁灭记忆、已成空白 提交于 2019-11-27 04:47:50

问题


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?

Thank you


More Details :

I have 4 or more mail boxes. Each has an number X of folders and subfolders (1 of them is a livelink box with millions of folders). Some are common box, and there are people who drag common mail.

I want to catch every time a mail is moved on a folder in livelink box.


回答1:


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.



来源:https://stackoverflow.com/questions/8009837/vba-outlook-event-moving-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!