Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook email body read

Status
Not open for further replies.

NoobyDooby

Technical User
Aug 24, 2010
2
GB
We are using software that monitors the event logs on several servers. The software sends an email alert that ends up in a subfolder within the Inbox. Because there are so many of these emails I need to be able to make a copy of the email body and place that into a text file as a single line and save it so that the text file can then be investigated. Any idea on how to code this. I do not know how to code Outlook VBA, but do understand VB. Thanks
 
Don't know about outlook vba to save to a text file, but you could, if you have MS-Access link in the outlook folder and it will create what looks like a table with each item in its own col, so you would have a col for To, From, Subject, Body, etc. Then you could write a query to filter and/or parse out the data that you need and export this as a text file, or if you don't need to parse, you can export the linked table as a text file. I used this technique to extract certain elements from an email body from mail sent from vendors. Converting long lines of text into distinct data elements using left, mid, right and instr functions. Since you set up a specific outlook subfolder, you could link directly to that and then you won't be seeing all the other emails.
 
This is actually surprisingly not easy, but only because the events in Outlook are... difficult.

-->UNTESTED<--
Code:
Private Sub Application_NewMailEx(ByVal EntryIDs As String)
'This will capture a reference for EVERY post item, not just the first

Dim olApp As Outlook.Application
Dim mapiNS As NameSpace
Dim idArray() As String
Dim postObject As Object
Dim ts as TextStream
Dim fs as Scripting.FileSystemObject
Dim f as Object

Set olApp = Outlook.Application
Set mapiNS = olApp.GetNamespace("MAPI")

Set fs = CreateObject("Scripting.FileSystemObject")
'Change to your log file. If you want to create a new file, I believe it's fs.CreateFile?
Set f = fs.GetFile("C:/Path/LogFile.txt")
Set ts = f.OpenAsTextStream(ForAppending)

'Here's where we start to loop through the post items
idArray = Split(EntryIDs, ",")
For i = 0 To UBound(idArray)
    Set postObject = mapiNS.GetItemFromID(idArray(i))
    If postObject.Class = olMail Then
        'You need to determine a logic to specify your emails
        If (InStr(postObject.Subject, "LogFileExampleString") > 0 Then
            'And how you want the line formatted
            ts.writeline Format(Now(), "yyyy-mm-dd hh:mm.ss") &", "& Replace(postObject.Body, Chr(10), "")
        End If
    End If
Next i

Set olApp = Nothing
Set mapiNS = Nothing
Set inboxFolder = Nothing
Set taskFolder = Nothing
Set postObject = Nothing
Set request = Nothing
Set fs = Nothing
Set f = Nothing
Set ts = Nothing

End Sub
Place this in your ThisOutlookSession section, and remember to save the project.

Like I said, this is untested. If you have problems, reply back with the line that gives error, or which erroneous action the process is doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top