2

At work, when we send out an e-mail we have to type in the BCC field a certain address as well as click on the "message options" and type in "direct replies to:" another address.

I want to add a button to the ribbon that will: Open up a reply based on the e-mail I have highlighted in the "inbox" pane, automatically add the e-mail to the BCC field AND automatically set the message to "direct replies to:"

So far I have come up with this:

Sub ReplyUW()
Dim mail As MailItem
Set mail = ActiveInspector.CurrentItem

mail.ReplyRecipients.Add ("XXXX@email.com")
mail.ReplyRecipients.Add ("XXXX@email.com")
mail.Recipients.ResolveAll
End Sub

This sets it up to "direct replies to:" but only if I have the message opened.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dan Slocum
  • 25
  • 1
  • 4
  • Ok so what have you actually tried abd which part(s) are you stuck with? Try breaking it down to smaller pieces and you may be able to solve this (or at least part of it) on your own. – David Zemens Sep 14 '14 at 22:50
  • Well I've been trying to figure out the "set mail = active inspector.currentitem" line first. I understand this is selecting the current e-mail that's "open," but I first want to tell it to select the highlighted item in the inbox and then open a reply to it... I think once I got that part down I maybe can figure out the rest pretty easily? Let me do some more research and get back to you :) This may reveal just how limited I am in VBA. I am actually looking to find a class at a local CC to learn it more in-depth. – Dan Slocum Sep 15 '14 at 13:27
  • OK so you're going to need to use `Application.ActiveExplorer.Selection` instead of `ActiveInspector`. Give me a minute and I'll try to point you in the right direction. – David Zemens Sep 15 '14 at 15:34

1 Answers1

2

Let's start with something like this. As I mentioned in the comments, you can use the ActiveExplorer instead of the ActiveInspector. This macro is set up to run on all selected items, so you could select more than one email, and "reply" to all of them automatically.

Sub test()

Dim m As MailItem 'object/mail item iterator
Dim recip As Recipient 'object to represent recipient(s)
Dim reply As MailItem 'object which will represent the reply email

'Loop over each SELECTED item:
For Each m In Application.ActiveExplorer.Selection
    If m.Class = olMail Then
    Set reply = m.reply

    'Adds a "direct replies to" address:
    reply.ReplyRecipients.Add "XXXX@email.com"

    'Adds BCC recipient to the reply:
    Set recip = reply.Recipients.Add("someone_else@email.com")
    recip.Type = olBCC '3

    reply.Save 'saves a draft copy to your SENT folder
    reply.Send

    End If
Next

End Sub

Credit where it's due, I did not know how to add a BCC recipient, but I found out how to do that here:

Add BCC to email with VBA in Outlook 2013

Adding a button to the ribbon is probably more involved and is certainly more complicated than maybe necessary, and if you do desire to do that, I would recommend it should be a new question. Ribbon/XML customization is not "simple" VBA anymore.

For the time being, if you simply enable the Developer ribbon, then you can access this macro from the Macros menu:

enter image description here

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Oh, man.. perfect! I got as far as this: `Sub Reply() Dim mail As MailItem Set mail = ActiveInspector.CurrentItem mail.ReplyRecipients.Add ("xxxx@xxx.com") mail.Recipients.ResolveAll Dim myOlApp As Outlook.Application Dim myItem As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient ' no need for new Object, get current Object Set myOlApp = GetObject(, "Outlook.Application") Set myItem = myOlApp.ActiveInspector.CurrentItem With myItem .BCC = "xxxxx@xxxxx.com" End With` Ok I am failing at formatting my comment, but thanks so much! – Dan Slocum Sep 16 '14 at 02:19
  • Cheers for sticking at it! Right it is hard to read your code here in the comments but there are a few issues: **1:** you don't need to use `GetObject`. By default, `Application.` refers to the Outlook.Application class, and since it's running, there's no need to "Get" it, it's already in scope :) **2:** the `.BCC` field is read-only so you can't assign to it that way (I figured that out this morning while trying to answer your Q!). – David Zemens Sep 16 '14 at 02:24
  • Thanks man! I noticed your macro just automatically replies, I am looking to have it just open a reply message with all the BCC/Direct replies to filled in and I can then type my message. I am going to keep working at your macro and get it figured out myself so I learn a bit more, but you totally helped make some connections for me. Thanks so much! – Dan Slocum Sep 17 '14 at 03:22
  • Instead of `reply.Send`, do `reply.Display`. – David Zemens Sep 17 '14 at 11:18
  • BTW, if you are trying to learn, check out the [Outlook object model reference](http://msdn.microsoft.com/en-us/library/office/ff870566(v=office.14).aspx). These can be a bit cryptic at first, and you will never need to use most of it, but it's a great way to see what you can do with certain objects, like the [MailItem](http://msdn.microsoft.com/en-us/library/office/ff861252(v=office.14).aspx). Good luck!! – David Zemens Sep 17 '14 at 11:53