简体   繁体   中英

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

The PropertyAccessor class has some limitations. One of them is that string properties are limited in size depending on the information store type. You need to use a low-level API - Extended MAPI for reading properties without limitations introduced by the OOM. The OpenProperty method of the IMAPIProp interface returns a pointer to an interface that can be used to access a property. Here is what MSDN library states:

The IMAPIProp::OpenProperty method provides access to a property through a particular interface. OpenProperty is an alternative to the IMAPIProp::GetProps and IMAPIProp::SetProps methods. When either GetProps or SetProps fails because the property is too large or too complex, call OpenProperty.

Or you may consider using third-party wrapper around Extended MAPI (for example, Redemption).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM