Search code examples
pythonvbaoutlookoffice365pywin32

Is there a way to intercept a SEND in Outlook to do further verification on the recipients?


I need to add a macro to Outlook so that when I press SEND to send an e-mail, it makes a further check on the recipients and if at least one of these falls under a specific list, it asks me for further confirmation before sending the e-mail. This is to avoid that I accidentally send an email to someone who shouldn't receive it, as sometimes I don't notice that outlook has pre-filled one of the destination fields with a different email than I wanted, as I am visually impaired.

I'm not very experienced in VBA and I don't even know if it's possible to intercept a SEND, but I know Python well and it would be perfect if I could use a macro in Python with pywin32. Alternatively, however, I can get by with VBA as well. I need it to work with both Outlook Desktop and Outlook 365 from the browser.

Do you think it is possible? And if so, how?


Solution

  • In general, it is possible. You can handle the ItemSend event of the Outlook Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. For example, in VBA you can use the following code (to add an event handler correctly you need to choose the Application object from the drop-down list on the left side and the ItemSend event on the right hand side):

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
     Dim prompt As String 
     prompt = "Are you sure you want to send " & Item.Subject & "?" 
     If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then 
       Cancel = True 
     End If 
    End Sub
    

    As you can see an instance of the Item which represents the object which is being sent is passed as a parameter to the event handler. You may check all the Recipients property which returns a Recipients collection that represents all the recipients for the Outlook item.

    it would be perfect if I could use a macro in Python with pywin32.

    You can automate Outlook from any programming language including Python. But you need to get the code running with Outlook every time you want to handle outgoing emails. Only VBA can be run with Outlook (when it starts). Also you may consider creating a COM add-in for Outlook if you need to deploy your solution on multiple machines. That is for COM add-ins were introduced.

    I need it to work with both Outlook Desktop and Outlook 365 from the browser.

    VBA (as well as COM add-ins) is for Outlook Desktop only. If you need to support multiple platforms (OWA or MacOS) you need to create a web add-in for Outlook where you could also handle the ItemSend event, see On-send feature for Outlook add-ins.

    Read more about Outlook web add-ins on the Build your first Outlook add-in page.