PDA

View Full Version : Getting Email Headers



u6ik
02-16-2007, 02:52 AM
I've been trying to obtain the email header from messages in Outlook. I have done some investigation and found that in outlook they are stored separately and require use of the CDO library to get hold of the header. So, I used Tools-Reference to enable the CDO Win 2000 library, cdosys.dll and employed the code below - but on compiling in the Outlook VBA Editor it gives me errors in all references to that CDO library. Can anyone assist here? Many thanks

Public Function InternetHeaders() As String

Dim objOutlook As Outlook.Application
Dim objItem As Outlook.MailItem
Dim objCDO As MAPI.Session
Dim objMessage As MAPI.Message
Dim objFields As MAPI.Fields
Dim strID As String

Const CdoPR_TRANSPORT_MESSAGE_HEADERS = &H7D001E

On Error Resume Next

' Instantiate an Outlook Application object.
Set objOutlook = CreateObject("Outlook.Application")

'Find the current email item and get its EntryID
Set objItem = objOutlook.ActiveInspector.CurrentItem
strID = objItem.EntryID

'Then set up a CDO Session using a piggy-back login
Set objCDO = CreateObject("MAPI.Session")
objCDO.Logon "", "", False, False

'Now get the item as a CDO Message
Set objMessage = objCDO.GetMessage(strID)

'Now get the headers from the message
Set objFields = objMessage.Fields
InternetHeaders = objFields.Item(CdoPR_TRANSPORT_MESSAGE_HEADERS).Value

'Now that the headers are captured in a string you can do whatever you want with them
objCDO.Logoff

Set objFields = Nothing
Set objMessage = Nothing
Set objCDO = Nothing
Set objItem = Nothing
Set objOutlook = Nothing
End Function

u6ik
02-16-2007, 07:25 AM
Okay, so it looks like this code is falling down on the creation of the MAPI.Session object

Dim CDOSession As Object
Set CDOSession = CreateObject("MAPI.Session")

Even though I have the CDO library referenced in the Tools-Reference area of the VBA editor. Anyone have any ideas?

u6ik
02-16-2007, 10:39 AM
Problem solved. The CDO is not installed as standard in Outlook 2003! After install all code works fine.