Search code examples
vbaemailoutlookoutlook-2010

Truncated Outlook email headers retrieved using VBA


I want to access the email headers in Outlook 2010. I use the code below but unfortunately the result contains only the first 252 characters of the header. Any suggestion on what I'm doing wrong?

Dim strHeader As String
strHeader = GetInetHeaders(olItem)
MsgBox "Truncated string: " & strHeader 

and

Function GetInetHeaders(olkMsg As Outlook.MailItem) As String
    ' Purpose: Returns the internet headers of a message.'
    ' Written: 4/28/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: 2007'
    Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
    Dim olkPA As Outlook.PropertyAccessor
    Set olkPA = olkMsg.PropertyAccessor
    GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
    Set olkPA = Nothing
End Function

Solution

  • Msgbox is not a good method of proving truncation. Text can be legitimately truncated.

    Text in a mailitem does not appear to be truncated. At least I do not notice cut off information.

    Private Sub Test_GetInetHeaders()
    
    Dim olNewmail As mailItem
    Dim strHeader As String
    Dim olItem As mailItem
    
    Set olItem = ActiveInspector.currentItem
    strHeader = GetInetHeaders(olItem)
    
    Set olNewmail = CreateItem(olMailItem)
    olNewmail.body = strHeader
    olNewmail.Display
    
    MsgBox "Truncated string if limit exceeded: " & strHeader
    
    ExitRoutine:
        Set olItem = Nothing
        Set olNewmail = Nothing
    
    End Sub
    
    Function GetInetHeaders(olkMsg As outlook.mailItem) As String
        ' Purpose: Returns the internet headers of a message.'
        ' Written: 4/28/2009'
        ' Author:  BlueDevilFan'
        ' Outlook: 2007'
        Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
        Dim olkPA As outlook.propertyAccessor
        Set olkPA = olkMsg.propertyAccessor
        GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
        Set olkPA = Nothing
    End Function