0

I'm trying to create a script to open the Reply All window automatically when a specific email is received.

Also for it to include specific text in the body, I don't want the email to be send just want the window to open.

I have tried the following:

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

This will Open the email that is selected in the inbox rather than the email that was just received.

How can I change this to Reply All on the specific email?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Evan
  • 11
  • 4
  • Please post what you have tried. We are not a code delivery service, we are here to help solve technical problems. – Milk Aug 31 '17 at 19:12
  • https://stackoverflow.com/questions/11263483/how-do-i-trigger-a-macro-to-run-after-a-new-mail-is-received-in-outlook – Tim Williams Aug 31 '17 at 19:57

1 Answers1

1

Should be something like this

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

Code goes under ThisOutlookSession

0m3r
  • 12,286
  • 15
  • 35
  • 71