问题
I overwrite the Application_NewMail() function, in order to do something with the incoming mail.
If the incoming mail matches given condition, then I want to do olMail.Subject = "Mymark" + olMail.Subject or I do olMail.Categories = "MyMark".
But it seems that I do this too late, because the mail is already in inbox and theese changes are not propagated.
Private Sub Application_NewMail()
Dim olFld As Outlook.MAPIFolder
Set olFld = Outlook.Session.GetDefaultFolder(olFolderInbox)
olFld.Items.Sort "[ReceivedTime]", False
Dim olMail As Outlook.MailItem
Set olMail = olFld.Items.GetLast
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim doc As Variant
Set olMail = olFld.Items.GetLast
Set Reg1 = New RegExp
With Reg1
.Pattern = "[^0] (x ERROR)"
.Global = True
End With
If Reg1.Test(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
olMail.Subject = "mymark" + olMail.Subject
olMail.Categories = "XYZ"
Next
End If
End Sub
回答1:
Work with Application.NewMailEx event (Outlook)
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim Item As Object
Set Item = Session.GetItemFromID(EntryIDCollection)
Item.Subject = "mymark " & Item.Subject
Item.Save
Debug.Print Item.Subject
End Sub
来源:https://stackoverflow.com/questions/58080125/change-mail-item-within-the-application-newmail-event