This macro is made for a opsmgr management pack, soon to be published on this blog.

It is written in VBA in outlook 2010 (could work in other version too, but it is not tested).

It shows how to create a macro function, that is usable in a rule.

1. Enable Developer Ribbon to be able to create scripts


2. Open visual Basic


3. Select “ThisOutlookSession”


4. Type/ this Sub into Visual Basic. Changed the strExportPath to the folder you want to use.

Sub SaveMessageOnRule(Item As Outlook.MailItem)
	Dim strExportPath As String
	strExportPath = "C:\Temp\Mails\"
	Dim FileName As String
	FileName = strExportPath & Replace(Replace(Replace(Now & "_" & Item.EntryID & ".txt", ":", "_"), "-", "_"), " ", "_") 'Create filename and path, replace : with _, - with _ and space with _.
	Dim FileNum As Integer
	FileNum = FreeFile ' next file number
	Open FileName For Output As #FileNum ' creates the file if it doesn't exist
	Print #FileNum, Item.Body ' write information at the end of the text file
	Close #FileNum ' close the file
End Sub

5. Create rule in outlook


6. Select the coditions you want. Or none to process all emails


7. Select “run as Script” in the Select actions window, and choose the sub you have just created


8. Define exceptions if you have any, otherwise leave it blank.


9. Give your rule a name and save it.


10. You are now ready to setup the managementpack