5
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "example@email.com"
objMail.cc = "example2@email.com"
objMail.Subject = "Mail test"
objMail.HTMLBody = "This is my message"
unload me
objMail.Display
Set objMail = Nothing
Set objOutlook = Nothing

I am trying to add in another function that help to reply a selected email but can't figure out how I can mix this with Item As Outlook.MailItem I understand that replying an email will require that.

So I would like to know how I can add on so that I can select an email, execute the macro and it will input the recipient email into objMail.To and recipient's body into objMail.HTMLBody

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Zheng Yi Chew
  • 83
  • 1
  • 2
  • 7
  • When reply use `Dim Item As Outlook.MailItem` then `Item.Reply` or `Item.ReplyAll` – 0m3r Aug 04 '15 at 20:11
  • Hi Omar, thanks. But it doesn't work. Does that function 'Dim Item As Outlook.MailItem' help to detect what email I have selected as well? – Zheng Yi Chew Aug 04 '15 at 20:16
  • You just want to reply to selected email? – 0m3r Aug 04 '15 at 20:19
  • Yes, sorry if I misinterpreted my question. My actual plan is to select an email in inbox, and click the macro button so that it will display exactly what is shown just like when you click the reply button, but the only difference is to input my message(using objMail.HTMLBody) into the email as well. – Zheng Yi Chew Aug 04 '15 at 20:30

1 Answers1

6

To simply Reply or ReplyAll selected messages try the following.

Option Explicit
Sub ReplyMSG()
    Dim olItem As Outlook.MailItem
    Dim olReply As MailItem ' Reply
    Dim olRecip As Recipient ' Add Recipient
    
    For Each olItem In Application.ActiveExplorer.Selection
        Set olReply = olItem.ReplyAll
        Set olRecip = olReply.Recipients.Add("Email Address Here") ' Recipient Address
        olRecip.Type = olCC
        olReply.HTMLBody = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
        olReply.Display
    
        'olReply.Send
    Next olItem
End Sub

To hide the recipient use BCC Example

olRecip.Type = olBcc

To add multiple recipient just add

Set olRecip = olReply.Recipients.Add("Email Here")
Set olRecip = olReply.Recipients.Add("Email Here")
Set olRecip = olReply.Recipients.Add("Email Here")

With out Recipient try 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 = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
        olReply.Display

        'olReply.Send
    Next olItem
End Sub
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Thanks! However, the recipients email is not shown by default. Is there way to put it in without keying? – Zheng Yi Chew Aug 04 '15 at 20:46
  • @ZhengYiChew see my updated answer, - the last code I have removed the recipient, is that what you asking? – 0m3r Aug 04 '15 at 21:12
  • Keep in mind that you cannot concatenate 2 HTML strings when setting the `HTMLBody` property - your text needs to be inserted after the `` tag. – Dmitry Streblechenko Apr 27 '22 at 23:38
  • Works perfectly on my inbox items but when I select anything from my junk and run it I end up getting the error "Run-time error '91': Object variable or With block variable not set"? – MrJoshFisher Apr 28 '22 at 09:04