This was knocked up in a bit of a hurry but it seems to work.
First of all, in VBA add a reference to the Microsoft Word Object Library (
Tools >
References).
Paste the code in your
ThisOutlookSession's code window. Change the bit in red to point to folder where you want the Word documents saved. The filename is made up from the word "Mail" plus the date and time the macro ran and a one-up serial number per email, like: "Mail_21Feb11_230113_0001" (.doc or.docx). This is to ensure we get a unique filename for every email.
The bit in pink is where the contents of the email get copied to the Word document. If you want to
copy more stuff, just add more wordRange.InsertAfter commands.
The comment in green indicates where you can do some filtering on the incoming mail: if you start typing
If objMailItem. you will get a drop-down list of the various properties you can test. Leave that for later - just get the code working first before you start modifying it.
Code:
Option Explicit
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim objMailItem As MailItem
Dim arrMailItems() As String
Dim iCount As Integer
Const SavePath As String = "[COLOR=red][B]C:\Temp\[/B][/COLOR]"
Dim ThisDocument As String
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range
ThisDocument = [COLOR=blue][B]"Mail_" & Format(Now(), "ddmmmyy_hhnnss")
[/B][/COLOR]
arrMailItems = Split(EntryIDCollection, ",")
For iCount = 0 To UBound(arrMailItems)
Set objMailItem = Application.Session.GetItemFromID(arrMailItems(iCount))
[COLOR=green] ' here you can [COLOR=green]check objMailItem.SenderName or objMailItem.Subject[/COLOR] here (for example)[/COLOR]
Set wordApp = CreateObject("Word.Application")
With wordApp
.WindowState = Word.WdWindowState.wdWindowStateMaximize
.Documents.Add ("normal.dotm")
Set wordDoc = .ActiveDocument
Set wordRange = wordDoc.Range
[COLOR=#ff00ff][B] wordRange.InsertAfter objMailItem.Body
[/B][/COLOR] .ActiveDocument.SaveAs SavePath & ThisDocument & "_" & Right("000" & CStr(iCount + 1), 4)
.ActiveDocument.Close
.Application.Quit
Set wordDoc = Nothing
Set wordApp = Nothing
End With
Next iCount
End Sub
Let me know how it goes?