Get email header from current email (Inspector)

Add-in Express™ Support Service
That's what is more important than anything else

Get email header from current email (Inspector)
 
Lance Friedemann




Posts: 24
Joined: 2017-02-10
This took me forever to figure out. So for anyone else interested, this is how you get the Outlook email header information from the current email:


Put this code in your Form1, not in your AddinModule!



    Friend Function GetEmailHeader(ByVal InspectororExplorer As Object) As String

        Dim item As Object = Nothing
        If TypeOf InspectororExplorer Is Outlook.Inspector Then
            Try
                item = CType(InspectororExplorer, Outlook.Inspector).CurrentItem
            Catch
            End Try
        End If

        If item Is Nothing Then
            Return ""
        Else
            Dim propertyAccessor As Outlook.PropertyAccessor = item.PropertyAccessor
            Dim hdrStr As String = _
                item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
            Marshal.ReleaseComObject(propertyAccessor)
            Marshal.ReleaseComObject(item)
            Return hdrStr
        End If

    End Function

    'Then to call the Function when your form loads:

    Private Sub ADXOlForm1_Ticket_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Get this email Header and store in a variable
        Dim MeHdr As String = GetEmailHeader(InspectorObj)
        Marshal.ReleaseComObject(InspectorObj)

        'Or you could put it in a textbox:
        Me.TextBox1.Text = GetEmailHeader(InspectorObj)
        Marshal.ReleaseComObject(InspectorObj)

    End Sub



Cheers!
Posted 11 Feb, 2017 22:51:16 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Hello Lance,

Lance Friedemann writes:
Marshal.ReleaseComObject(InspectorObj)


I assume you use an ADXOlForm, not System.Windows.Forms.Form. If so, the code line above may produce many issues as it releases the COM object on which Add-in Express relies! And the issues may not occur immediately! Don't release ADXOlForm.InspectorObj and ADXOlForm.ExplorerObj!


Andrei Smolin
Add-in Express Team Leader
Posted 13 Feb, 2017 05:04:58 Top
Lance Friedemann




Posts: 24
Joined: 2017-02-10
Thank you for the debug, I've adjusted the code:



Friend Function GetEmailHeader(ByVal InspectororExplorer As Object) As String 
 
    Dim item As Object = Nothing 
    If TypeOf InspectororExplorer Is Outlook.Inspector Then 
        Try 
            item = CType(InspectororExplorer, Outlook.Inspector).CurrentItem 
        Catch 
        End Try 
    End If 
 
    If item Is Nothing Then 
        Return "" 
    Else 
        Dim propertyAccessor As Outlook.PropertyAccessor = item.PropertyAccessor 
        Dim hdrStr As String = _ 
            item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E") 
        Marshal.ReleaseComObject(propertyAccessor) 
        Marshal.ReleaseComObject(item) 
        Return hdrStr 
    End If 
 
End Function 
 
'Then to call the Function when your form loads: 
 
Private Sub ADXOlForm1_Ticket_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
 
    'Get this email Header and store in a variable 
    Dim MeHdr As String = GetEmailHeader(InspectorObj) 
 
    'Or you could put it in a textbox: 
    Me.TextBox1.Text = GetEmailHeader(InspectorObj) 
 
End Sub 

Posted 13 Feb, 2017 10:28:30 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Good!


Andrei Smolin
Add-in Express Team Leader
Posted 14 Feb, 2017 02:55:03 Top