Reply to Recipient
April 15, 2009 • JP • No Comments • Rate This Article
• Links to this article
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).
' 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
Previous Post: Excel Tutorial Series – VBA Macros, Part One Of Two
Next Post: Add-in Updates




Speak Your Mind
Tell us what you're thinking...Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].