VSTO Outlook加载项 - 已发送项目的处理程序并不总是有效

时间:2017-04-13 20:01:06

标签: vb.net vsto outlook-addin

我有一个outlook addin处理某种类型的已发送电子邮件。对于某种类型的电子邮件,用户必须填写包含一些信息的表单,然后应用程序将电子邮件作为MSG导出到文件系统中的目录树

我有一个带有按钮的功能区,该按钮为发送的电子邮件设置标记(userproperty),以便addin知道必须保存哪个电子邮件:

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

在插件启动中,我有(代码段):

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

和我的itemadd例程:

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

正如您所看到的,只要电子邮件具有此“SALVARAPIGES”用户属性,就必须将其保存到目录树中的MSG。但有一半的用户说这不起作用(表格不是POPPED),但每当我测试它时,它都有效。我对VSTO及其背后的所有概念都很陌生......任何人都能指出方向吗?

我尝试过使用NameSpace.SendAndReceive方法,但他们一直在抱怨。

提前感谢!

2 个答案:

答案 0 :(得分:1)

引发事件的对象(sentItems)被声明为局部变量。一旦GC启动,它就会被释放,不会再引发任何事件。在类级别声明变量以确保它保持活动状态。

答案 1 :(得分:1)

您需要在类级别声明源对象,以防止它被垃圾回收器滑动:

Dim sentItems As Outlook.Items

Private Sub ThisAddIn_Startup() Handles Me.Startup
 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