Search code examples
vbaemailoutlook

Automatically forward email by VBA Outlook


I am using the code below to automatically forward email. I added an Outlook rule already.

  1. Outgoing email address
    Currently: can only send 1 email
    Desire: send to many different emails

  2. Email content
    Currently: no email content
    Desired: add a command about email content including ( title mail + any content )

Sub AutoForwardAllSentItems(Item As Outlook.MailItem)
Dim strMsg As String
Dim myFwd As Outlook.MailItem

Set myFwd = Item.Forward

myFwd.Recipients.Add "[email protected], [email protected]"
myFwd.Recipients.Add "[email protected]; [email protected]"
myFwd.Recipients.Add "[email protected] [email protected]"

xStr = "<p>" & "Hi, Your email has been received. Thank you!" & "</p>"
myFwd.HTMLBody = xStr & .HTMLBody

myFwd.Send
Set myFwd = Nothing
End Sub

Solution

  • You are not specifying where you are getting .HTMLBody from, but I assume it to be from the Item argument.

    You can add that in like this:

    myFwd.Recipients.Add "[email protected]"
    myFwd.Recipients.Add "[email protected]"
    myFwd.Recipients.Add "[email protected]"
    
    xStr = "<p>Hi, Your email has been received. Thank you!</p>"
    myFwd.HTMLBody = xStr & Item.HTMLBody