There are a few preparation steps to go through in order to make this work. Let’s go through them.
Make sure the Outlook instance on which you want to implement this has the Developer option enabled. Go to File>Options>Customize Ribbon and tick the Developer option:
Note: On Outlook 2007, you can’t make the Developer option appear on the main Ribbon but you can activate it on a message’s >Toolbar. To do so, create a new message (or open an existing one), click on Office button (upper left corner)>Editors >options>Popular and tick the Show Developer tab in the Ribbon option.
Make sure Outlook is allowed to run macros (they are disabled by default). Go to the Developer option on the Toolbar and click the Macro Security option :
Note: On Outlook 2007, the VB, Macros, Script Debugger and Macro security options may be greyed out. This is due to missing certificates in the Outlook/Office setup. To remedy:
- Control Panel->Programs and Features
- Right-click Microsoft Office
- Click Change
- Continue on Add or Remove Features
- Scroll down and expand Office Shared Features
- For the two Features “Digital Certificate for VBA Projects” and “Visual Basic for Applications” select ‘Run from my Computer‘
Close Outlook and launch it again
Go to the Developer option on the Toolbar and click the Visual Basic option. In the new Window that pops up, click File>Import File... and import the attached .bas file. The code should display on the right hand side of the window. In the Public Sub SaveMsg method, change the line sPath = "C:\Test\Emails\" to match the hot folder where you want to save the messages. Then click File>Save and close the Visual Basic window.
Note: On Outlook 2007, the Set Sender = mail.Sender line in the macro returns an error as the Sender is not a property of the mail object. Just comment out the entire condition that checks for the EX string in the SenderMailType property and replace it simply with GetSenderSMTPAddress = mail.SenderEmailAddress.
Close Outlook and open Regedit. Create a DWORD32 key named EnableUnsafeClientMailRules under either:
HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security (for Outlook 2016) or
HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Security (for Outlook 2013)
At this stage, you have code that exports messages. All you need to do now is tell Outlook when to run that macro by using the very efficient Outlook Rules. Starting with Outlook 2013, Outlook no longer allows you to create a Rule that runs a macro for security reasons. However, modifying a particular registry key allows you to change this default behaviour.
Close Regedit and open Outlook yet again
You can now create the Outlook Rule. Go to File>Rules and Alerts and create a new rule. The rule could be checking for messages from a specific sender, a particular subject, etc...
The only thing remotely remarkable about this new rule is that you will want to select the action named run a script and specify the Project1.SaveMsg method:
Outlook is now set up so that anytime a new email message comes in, the Rule Engine will evaluate your rule and trigger the export script if the rule is true.
Note: Outlook must me left running at all times if you want the Rule Engine to run on every email. If you close it, the process will not trigger until you launch the application again. If you take a quick look at the VBA code, you will notice that the attachments are saved first and the body is saved last. This allows you to create a Workflow process that only monitors the hot folder for files with .xml extensions (i.e. the file that contains the email body and the names of all attachments, which are saved with an arbitrary .att extension). Once the files have all been saved, the email is marked as Read and moved to the Deleted Items folder. You can tweak the code to your liking but for basic Email Capture from Outlook, this procedure should be all you need.