Partager via


Job/Life/Reputation protector 2.0

Back in January I posted some simple VBA code for adding an “are you sure?” type question to the Reply To All button in Outlook.  Since then I have received a few suggestions for improving the code, one of the most common of which was to add to the question box the list of names that the mail will be sent to.  So, as requested, you can find below the updated code! 

image 

Just place this code in a module in Outlook (you can get to the VBA editor by pressing ALT+F11) and then assign it a button on the toolbar:

Sub ReplyToAllGuard()

    On Error GoTo ReplyAllErr
    Dim myOlApp As Outlook.Application
    Dim myFolder As Outlook.MAPIFolder
    Dim myItem As Outlook.MailItem
    Dim ReplyMail  As Outlook.MailItem
    Dim i As Integer

    Set myOlApp = Application
    Set myFolder = myOlApp.ActiveExplorer.CurrentFolder

    iCount = myOlApp.ActiveExplorer.Selection.Count

    For i = 1 To iCount

        Set myItem = myOlApp.ActiveExplorer.Selection.Item(1)
        Set ReplyMail = myItem.ReplyAll

    Next i

    Dim mymsg As String
    Dim strRecipients As String
    Dim ReplyQ As Integer
   
    For Each strRec In ReplyMail.Recipients

        strRecipients = strRecipients & strRec & Chr(13)

    Next

    mymsg = "You just clicked Reply to All.  Are you sure that this is what you want to do?  The following recipients will receive this mail:" & _
            Chr(13) & Chr(13) & strRecipients

    ReplyQ = MsgBox(mymsg, vbYesNo, "Job/Life/Reputation protector")

    If ReplyQ = vbNo Then

        Set ReplyMail = Nothing
        Exit Sub

    Else

        myItem.UnRead = False
        ReplyMail.Display

    End If

ReplyAllErr:

    If Err.Number = -2147467259 Then

        MsgBox "The sender has prevented you from being able to reply to all recipients.", vbOKOnly, "Job/Life/Reputation protector"

    End If

    Exit Sub

End Sub

 

I also made one another change to the code in order to prevent receiving the error “A program is trying to access e-mail address information stored in Outlook”.  This error is generated because Outlook has detected a possible security risk because some ‘unknown’ VBA code is trying to harvest the e-mail addresses from an e-mail; in this case though, we actually want it to!

 image

Basically, instead of initiating a new Outlook session in memory, the code now reuses the existing instance of Outlook, thereby not generating a security problem as the message box is suggesting.

 

Edit: I amended the code to fix an oversight by me where the code did not clean up after itself!  Also, the mail now marks itself as read after you reply - Thanks JP for pointing it out.

Comments

  • Anonymous
    January 01, 2003
    Agreed, thanks JP. I have to confess that I only discovered this tip a short while ago, which is why I also included it in this code update. Regards, Daniel

  • Anonymous
    January 01, 2003
    Thanks JP, I already made the changes!

  • Anonymous
    November 11, 2008
    You should never use CreateObject when coding natively in Outlook. If you always generate your objects from the Outlook.Application object, you won't get the security prompt. Take care, JP

  • Anonymous
    November 11, 2008
    Daniel, I would also note that creating a reply programmatically doesn't mark the message as Read. You would need to do that by calling myItem.Unread = False. I notice your code creates the reply message before asking the "Are you sure?" question to the end user, but doesn't destroy the reply message if they answer "no". FYI- Here's another way to disable "Reply To All", by preventing your recipients from even accessing the option. Although I must say I've never tested it. >:) ActiveInspector.CurrentItem.Actions("Reply to All").Enabled = False Thx, JP