【问题标题】:Change mail item within the Application_NewMail Event在 Application_NewMail 事件中更改邮件项目
【发布时间】:2020-04-02 13:56:41
【问题描述】:

我重写了 Application_NewMail() 函数,以便对收到的邮件进行处理。

如果收到的邮件符合给定条件,那么我想做 olMail.Subject = "Mymark" + olMail.Subject 或者我做 olMail.Categories = "MyMark"。

但似乎我这样做太晚了,因为邮件已经在收件箱中,并且这些更改没有传播。

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

【问题讨论】:

  • 你能显示代码吗?
  • 我在上面添加了我的代码。
  • 您应该首先提供一个适当的主题来证明 RegEx 有效,但很可能是oMail.Save
  • @niton oMail.Save 是解决方案,但我不明白你的意思是什么:'你应该首先提供一个适当的主题来证明 RegEx 有效'
  • 只是对完整问题所需内容的建议。问题中的一个示例主题将证明 RegEx 不是问题的根源。我不得不假设 RegEx 是好的。

标签: vba outlook


【解决方案1】:

使用Application.NewMailEx event (Outlook)

NewMailEx 事件在新邮件到达收件箱时和客户端规则处理发生之前触发。您可以使用 EntryIDCollection 数组中返回的 Entry ID 来调用 NameSpace.GetItemFromID 方法并处理该项目。

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

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多