收到特定电子邮件时打开全部回复窗口

时间:2017-08-31 18:45:13

标签: vba outlook outlook-vba

我正在尝试创建一个脚本,以便在收到特定电子邮件时自动打开“全部回复”窗口。

另外,为了在正文中包含特定文字,我不希望发送电子邮件只是想要打开窗口。

我尝试了以下内容:

Option Explicit
Sub ReplyMSG()
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply

For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
        olReply.HTMLBody = "TEXT " & vbCrLf & olReply.HTMLBody
    olReply.Display

    'olReply.Send
Next olItem
End Sub

这将打开在收件箱中选择的电子邮件,而不是刚刚收到的电子邮件。

如何在特定电子邮件中将此更改为全部回复?

1 个答案:

答案 0 :(得分:0)

应该是这样的

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    Dim olMsg As mailitem

    If TypeOf Item Is Outlook.mailitem Then
        Set olMsg = Item
        ReplyMSG olMsg
    End If

End Sub

Public Sub ReplyMSG(ByVal Item As Outlook.mailitem)
    Dim olReply As mailitem ' Reply 

    If (InStr(1, Item.Body, "Text to Search", vbTextCompare) > 0) Then
        Set olReply = Item.ReplyAll

        olReply.HTMLBody = "TEXT " & vbCrLf & olReply.HTMLBody
        olReply.Display
        'olReply.Send
    End If

End Sub
  

代码位于ThisOutlookSession