Outlook, AddItem and passing the Mail item around

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

Outlook, AddItem and passing the Mail item around
 
Paul Forrester




Posts: 10
Joined: 2017-08-11
I have a problem with an app that saves emails, the issue is that I cannot seem to pass the Item to the save form.

This is the code, firstly I watch the SentItems folder (code to do that elsewhere), this work fine:


    Public Overrides Sub ItemAdd(ByVal Item As Object, ByVal SourceFolder As Object)

        Dim oForm As Form2
        oForm = New Form2
        oForm.MyInitialise(Item)

        oForm.Show()
        oForm = Nothing

    End Sub


The Form2 has this which works:


    Public Sub MyInitialise(EmailItem As Object)
        NarrativeText.Text = "Would you like to save the email entitled: " + EmailItem.subject

    End Sub


My issue is on the Save Email button code, where I cannot reference the EmailItem as an object?

This code is for testing as a full save routine exists but I needed to know if it was working at the most basic level:


    Public Sub SaveSentEmail_Click(sender As Object, e As EventArgs) Handles Save_Sent_Email.Click

        Dim Msg$
        Msg = "The path is: " & EmailItem.Subject & vbCrLf
        MsgBox(Msg)

    End Sub


How can I reference the email?


Thank you
Posted 14 Aug, 2017 08:05:36 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Paul,

You can store the EmailItem object in a private field of your form, e.g.:


Private _emailItem as Object = Nothing

Public Sub MyInitialise(EmailItem As Object) 
    NarrativeText.Text = "Would you like to save the email entitled: " + EmailItem.subject 

    Me._emailItem = EmailItem
 
End Sub


Another possible solution is to store the EntryID property of the mail item instead of its reference and get the mail item when needed by using the Namespace.GetItemFromID() method.
Posted 14 Aug, 2017 09:49:45 Top
Paul Forrester




Posts: 10
Joined: 2017-08-11
Hi Dmitry,

I tried as suggested and get this error:

User added an image


Any thoughts?
Posted 15 Aug, 2017 04:18:59 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Paul,

The "COM object has been separated..." error occurs because the Add-in Express code releases the Item argument of the ItemAdd method after executing it. So, the reference to a mail item in your form is really "has been separated". You can use the ShowDialog method instead of Show to show your form. Or, as I mentioned in my previous post, you can pass the EntryID property of the mail item instead of its reference to your form and get the mail item when needed by using the Namespace.GetItemFromID() method:


Public Overrides Sub ItemAdd(ByVal Item As Object, ByVal SourceFolder As Object) 
 
    Dim oForm As Form2 
    oForm = New Form2 
    oForm.MyInitialise(Item.EntryID) 
 
    oForm.Show() 
End Sub



Private _emailID as String = String.Empty
 
Public Sub MyInitialise(EmailID As String)  
    Me._emailID = EmailID 

    Dim mail as Outlook._MailItem = GetMailItem(Me._emailID)
    NarrativeText.Text = "Would you like to save the email entitled: " + mail.Subject  
    Runtime.InteropServices.Marshal.ReleaseComObject(mail)
  
End Sub 

Private Function GetMailItem(EmailID As String) as Outlook._MailItem

    Dim result As Outlook._MailItem = Nothing

    Dim ns As Outlook._NameSpace = AddinModule.CurrentInstance.OutlookApp.GetNamespace("MAPI")
    If (ns IsNot Nothing) Then

        result = ns.GetItemFromID(EmailID)

        Runtime.InteropServices.Marshal.ReleaseComObject(ns)
    End If

    Return result

End Function
Posted 15 Aug, 2017 05:40:38 Top
Paul Forrester




Posts: 10
Joined: 2017-08-11
Thanks for this, I may have solved it in the meantime with:


    Private _xemailItem As Object = Nothing

    Public Sub MyInitialise(EmailItem As Object)
        NarrativeText.Text = "Would you like to save the email entitled: " + EmailItem.Subject
        Me._xemailItem = EmailItem.EntryID
    End Sub



And as a test:

Private Sub SaveSentEmail_Click(sender As Object, e As EventArgs) Handles Save_Sent_Email.Click

        Dim Msg$
        Msg = "The path is: " & Me._xemailItem & vbCrLf
        MsgBox(Msg)


        Dim App As Outlook.Application
        Dim NS As Outlook.NameSpace
        Dim MailItem As Outlook.MailItem

        App = CreateObject("Outlook.Application")
        NS = App.GetNamespace("MAPI")
        NS.Logon()
        MailItem = NS.GetItemFromID(Me._xemailItem)
        MailItem.Display()

    End Sub

Posted 15 Aug, 2017 05:47:59 Top
Paul Forrester




Posts: 10
Joined: 2017-08-11
My code does indeed work now, thank you for the assitance with this Dmitry.
Posted 15 Aug, 2017 05:57:38 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Paul,

App = CreateObject("Outlook.Application")


You can get access to the AddinModule.OutlookApp property instead of creating a new Outlook.Application object. Also, pay attention to releasing Outlook objects by using the Marshal.ReleaseComObject() method. You can find more information about releasing COM objects in the following blog posts:
When to release COM objects in Office add-ins developed in .NET
How to properly release Excel COM objects
Posted 15 Aug, 2017 06:00:25 Top