Return To Sender Published 6th Apr 2016
Return to Sender
For many years I’ve used Hotmail’s web interface for personal emails, but during a recent email purge I switched to using a local Outlook installation to manage this account. This is much better, but there’s one snag: I also use Outlook for work emails.
The problem here is that when I click ‘New message’, Outlook guesses which account I want to send it from based on which folder I’m currently in. Unfortunately this is rarely related to the subject of a new email:
The result is that I occasionally send emails from my personal account to people in work, and vice versa. This was becoming annoying, and there’s no easy way to colour-code Outlook to protect against this. Fortunately it’s possible to use Outlook macros for this purpose. Here’s how:
First, enable the ‘Developer’ menu option. To do this, go to File => Options => Customise Ribbon and tick the ‘Developer’ option in the tab list on the right-hand side:
From the Developer ribbon, select ‘Visual Basic’. This should bring up a screen that looks something like the screenshot below:
Double-click ‘ThisOutlookSession’ to bring up the code module in the right-hand window:
This shows you the VBA (Visual Basic for Applications) code that runs behind Microsoft Outlook.
Now paste the following code into the right-hand window. (This code will stop any emails to a work recipient being sent from a personal account; you’ll need to edit the highlighted parts to suit your needs. In my case I wanted to stop emails being sent from my personal email address to any recipient address containing our company domain, ‘htilabs.com’.)
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim Recipients As Outlook.Recipients Dim Recipient As Outlook.Recipient Dim PropertyAccessor As Outlook.PropertyAccessor Dim email As String Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" ' Set personal email address. Const personalEmailAccount As String = "firstname.lastname@example.org" On Error GoTo ErrorHandler Set Recipients = Item.Recipients ' Loop through recipients. For i = 1 To Recipients.Count ' Get recipient's email address. Set Recipient = Recipients(i) Set PropertyAccessor = Recipient.PropertyAccessor email = PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) ' Check whether this email is being sent from a personal account. If Item.SendUsingAccount = personalEmailAccount Then 'Check whether any work addresses appear in the recipients list. If InStr(LCase(email), "htilabs.com") Then ' Ask for confirmation, cancelling message if necessary. If MsgBox("This appears to be a work email (recipients include '" + email + "'); " _ + "are you sure you want to send from a personal account?", _ vbYesNo + vbQuestion + vbDefaultButton2 + vbMsgBoxSetForeground, "Check Address") = vbNo Then Cancel = True Exit Sub End If End If End If Next i ' Checks complete; exit and send. Exit Sub ' Handle errors and cancel message (do not send). ErrorHandler: MsgBox "Error " + CStr(Err.Number) + ": " + Err.Description Cancel = True End Sub
Save and close the VBA window.
Finally, click ‘Macro Security’ in the Developer ribbon, click ‘Macro Settings’ and select ‘Notifications for all macros’. Close and restart Outlook and you should be presented with a box like this:
Select ‘Enable Macros’. You should find that next time you try to send a work email from your personal account, you’ll get a warning message:
Now it’s straightforward to adapt the code above for whatever uses you need; for example, I realised that most of my work emails do include at least one person within the htilabs.com domain, so I've also added a check for that; occasionally this generates a 'false positive' but in those cases I just click 'Yes' in the message box.
It’s also possible to run other checks, for example by analysing the email body, checking for attachments and so on. For more information, take a look at the Microsoft documentation for the Outlook ‘MailItem’ object: https://msdn.microsoft.com/en-us/library/office/dn320330.aspx .
And there you have it – no more work emails sent from the wrong account.
Next week: Let Outlook determine how many kisses to add to your email.blog comments powered by Disqus
HTI Labs is a software and technology consultancy company specialising in software innovation. Whether it is Big Data technology or a fresh approach to solving your business problems, we are the people to speak to.