Link to home
Start Free TrialLog in
Avatar of rr2r
rr2rFlag for United States of America

asked on

Outlook 2016 VBA Macro -Reply to email witha custom "from" address...

Hello,

I am trying to automate one of the processes for our marketing department and they would like to be able to respond to some emails with a group "from address". My initial idea was to create a shared account to which all of them would have access and channel said emails there. However I am wondering if similar thing could not be achieved with a Macro. It would need to create a replay message to currently selected email, and would also need to set the "from" address to something predetermined in Macro versus the default "from" address that the Outlook would be normally using. I have found the following script that accomplishes the reply part but I am not sure how to make modify it so that a custom "from" address would be used.

Thanks!

Script:

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
Avatar of Bill Prew
Bill Prew

Have you tried just setting olReply.Sender to the desired address you want it sent from?

MailItem.Sender Property (Outlook)


»bp
Avatar of rr2r

ASKER

This is a sample code I found that does the "SandAs" part so it is a matter of combining the two now I would imagine...

Sub CustomMailMessage()
 
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Recipient
Dim Recipients As Recipients
 
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(olMailItem)
 
Set Recipients = objOutlookMsg.Recipients
Set objOutlookRecip = Recipients.Add("alias@domain.com")
objOutlookRecip.Type = 1
 
objOutlookMsg.SentOnBehalfOfName = "sales@domain.com"
 
objOutlookMsg.Subject = "Testing this macro"
 
objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf

'Resolve each Recipient's name.
For Each objOutlookRecip In objOutlookMsg.Recipients
objOutlookRecip.Resolve
Next
 
'objOutlookMsg.Send
objOutlookMsg.Display
 
Set OutApp = Nothing
 
End Sub
Avatar of rr2r

ASKER

@Bill Prew - no I have not tried that.
Avatar of rr2r

ASKER

Not seeing any info as far as how to use that option... What is the proper syntax for this?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rr2r

ASKER

It does 90% of what I need. A small modification would make it perfect:

1) Remove the additional address from the CC field - I don't think we have a need for that.
2) Do not put anything in the body of the email automatically - the response will be customized every time.

Take a look at the image for the areas highlighted. When I tried to simply remove the lines with appropriate code the Macro stopped working altogether  - I am really not good at this VBA thing :(

Thanks!
ThisOne.PNG
Okay, this removes the additional recipients as well as any body text.

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

    For Each olItem In Application.ActiveExplorer.Selection
        Set olReply = olItem.ReplyAll
        olReply.SentOnBehalfOfName = "sales@domain.com"
        olReply.Display
        'olReply.Send
    Next olItem
End Sub

Open in new window


»bp
Avatar of rr2r

ASKER

Thanks!