2017-04-13 60 views
0

我有一個Outlook插件,處理某些類型的已發送電子郵件。對於某些類型的電子郵件用戶必須填寫一些信息的表格,然後應用程序將電子郵件作爲MSG導出到文件系統中的目錄樹VSTO Outlook加載項 - 處理程序發送郵件不總是工作

我有一個功能區,帶有一個按鈕,用於爲發送的電子郵件設置一個標誌(userproperty)所以插件知道哪些郵件必須保存:

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中。但有一半的用戶表示這不起作用(表格不是彈出窗口),但每當我測試它時,它都可以正常工作。我對VSTO及其背後的所有概念都很陌生......任何人都可以指出我的方向嗎?

我已經嘗試使用NameSpace.SendAndReceive方法,但他們不斷抱怨。

在此先感謝!

回答

1

引發事件的對象(sentItems)被聲明爲局部變量。一旦GC啓動,它就會被釋放,並且不會引發更多事件。在類級別聲明變量以確保它保持活動狀態。

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 
+0

感謝您的更完整的答案,但第一個答案是仍然不足以解決問題,所以我會給他信貸。但是非常感謝你! – fferrandini

相關問題