Reply to Recipient

A reader writes:

"I am CC'd on emails to which I need to respond to the "To" recipient only, not the sender. Can you create a macro for this?"

Here's what I came up with. This code sets a reference to the currently selected or opened MailItem (using the function I posted in Post email data to the Web). Then it loops through the Recipients Collection for that message until it finds the one that was in the To: field. A reply is crafted, with that recipient in the To: field for the new message.

Keep in mind if the message has multiple recipients in the To: field, the code only grabs the first one it finds. And in certain environments, Recip.Address might be a string of characters like

/o=myCompany/ou=Administrators/cn=Recipients/cn=myFriend

In that case, use Recip.Name instead, and immediately after that, call the ResolveAll Method of the Recipients Collection (NewMsg.Recipients.ResolveAll).

Sub ReplyToRecipient()
' Replies to To Recipient only

Dim Msg As Outlook.MailItem
Set Msg = GetMessage

' if a mailitem is not selected or open, exit
If Msg Is Nothing Then GoTo ExitProc

' loop through recipients to find To recipient, reply to that person only
Dim MsgRecips As Outlook.Recipients
Dim Recip As Outlook.Recipient
Dim NewMsg As Outlook.MailItem

Set MsgRecips = Msg.Recipients

For Each Recip In MsgRecips
  If Recip.Type = olTo Then
    Set NewMsg = Msg.Reply
    With NewMsg
      .To = Recip.Address
      .Display
    End With
    GoTo ExitProc
  End If
Next Recip

ExitProc:
Set NewMsg = Nothing
Set MsgRecips = Nothing
Set Msg = Nothing
Set obj = Nothing
End Sub

Function GetMessage() As Outlook.MailItem
' returns MailItem object reference to open/selected mail item

' if any error occurs, just exit
On Error GoTo ExitProc
Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set GetMessage = ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set GetMessage = ActiveInspector.CurrentItem
End Select

ExitProc:
End Function

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 2 Comments:

  1. Outlook n00b writes:

    Is there any way to make a similar script that automatically sends a templated email to the recipient upon being CC'd?

Comments on this article are closed. Why?

Site last updated: February 9, 2012