Search code examples
vb.netvstooutlook-addin

VSTO Outlook Add-In - Handler to Sent Items not always work


I have an outlook addin that handles some type of sent emails. For some kind of email users must fill a form with some info and then the app export the email as MSG to a directory tree in filesystem

i have a ribbon with a button that sets a flag ( userproperty) to the sent email so the addin knows which email must be saved:

Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
    Dim oProperties As Outlook.UserProperties
    Dim oProperty As Outlook.UserProperty


    oMail = Globals.ThisAddIn.Application.ActiveInspector.CurrentItem


    If Not oMail Is Nothing Then

        oProperties = oMail.UserProperties
        oProperty = oProperties.Add("SALVARAPIGES", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olYesNo)
        oProperty.Value = True
        oMail.Save()
        oMail.Send()
    Else
        MsgBox("Err")
        Exit Sub
    End If
End Sub

In the addin startup, i have (snippet):

Private Sub ThisAddIn_Startup() Handles Me.Startup
    Dim sentItems As Outlook.Items
    Dim sentFolder As Outlook.Folder
    Dim paisapiges As String
    Dim aux As String()
    Dim ns As Microsoft.Office.Interop.Outlook.NameSpace

    apigesIsLoaded = True

    'adiciono um trigger para que sempre que for enviado um email e for do tipo SALVARAPIGES, ele fará o tratamento de salvar o email.
    sentFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
    sentItems = sentFolder.Items
    AddHandler sentItems.ItemAdd, AddressOf itemadd

and my itemadd routine :

Sub itemadd(ByVal NewEmailItem As Object)
    Dim oProperties As Outlook.UserProperties
    Dim salvaApiges As Boolean
    Dim sentMessageItem As Outlook.MailItem = CType(NewEmailItem, Outlook.MailItem)
    Dim mainForm As New formSalvarApiges()

    salvaApiges = False
    If Not sentMessageItem Is Nothing Then
        oProperties = sentMessageItem.UserProperties
        For Each pr As Outlook.UserProperty In oProperties
            If pr.Name = "SALVARAPIGES" Then
                salvaApiges = True
                Exit For
            End If
        Next

        If salvaApiges Then
            mainForm.txtAssunto.Text = sentMessageItem.Subject
            mainForm.sAction = "Acao01"
            mainForm.sEntryId = sentMessageItem.EntryID
            mainForm.ShowDialog()
            mainForm.Close()
        End If
    End If
End Sub

as you can see whenever a email has this "SALVARAPIGES" userproperty it must be saved to MSG in a directory tree. But half users are saying this does not work ( THE FORM IS NOT POPPED), but whenever i test it, it works. I'm very new to VSTO and all concepts behind it...can anyone just point me the direction?

I have tried using the NameSpace.SendAndReceive method, but they keep complaining.

thanks in advance!


Solution

  • The object that raises the events (sentItems) is declared as a local variable. As soon as GC kicks in, it gets released and no more events are raised. Declare the variable on the class level to make sure it stays alive.