VSTO MailItem.Save错误“由于邮件已更改,因此无法执行操作”

时间:2017-09-13 15:52:42

标签: vb.net vbscript

我正在尝试使用资源管理器对象更改Outlook 2013中当前所选邮件的类别颜色,以获取当前所选项目。一切似乎都很好,除非我保存它给出上面提到的错误。我一直在寻找解决方案,没有任何想法的运气?谢谢。这是我在VB中的代码

   Private Sub exp_SelectionChange() Handles exp.SelectionChange ' errrrorr
    Try
        waitapprovemail = Application.Session.GetItemFromID(exp.Selection.Item(1).EntryID)



        if (CheckForRedCategory(waitapprovemail)) Then

            If (CheckToReleaseMail(waitapprovemail)) Then

                waitapprovemail.Categories = "Green Category"

                waitapprovemail.Save() ''' this gives the error


            End If
        End If

    Catch Exc As System.Runtime.InteropServices.COMException
        MsgBox(Exc.Message & "  " & Exc.Source)

    Catch exc As System.InvalidCastException
        MsgBox("Casting problem")
    End Try

End Sub

Private Function CheckToReleaseMail(mail As MailItem) As Boolean ' errrrrr
    ' check the id with the ids in the locked mail, if found id then check the other flag if it is false or true, if found true then set the category of that waitemail to empty "" else keep it 
    Dim r As Boolean = True
    Dim sarray As String()
    ' ofile2 = fso2.OpenTextFile("C:\Users\" & userName & "\Documents\Outlook Files\LockedMail.txt", 8, True) '8 for appending in arg2 0 for tristatefalse optional opens as ascii
    Try
        Using sr As New StreamReader("C:\Users\" & userName & "\Documents\Outlook Files\LockedMail.txt")
            Dim line As String
            Do
                line = sr.ReadLine()
                If (line.Equals("") Or line Is Nothing) Then
                    r = True
                    Continue Do
                Else
                    sarray = line.Split(",")
                    If (sarray.Count > 0) Then
                        If (sarray(0).Equals(mail.EntryID, StringComparison.InvariantCultureIgnoreCase)) Then
                            r = False
                            sr.Close()
                            mail.Close(OlInspectorClose.olDiscard)
                            Return r
                        End If
                    End If
                End If
            Loop Until line Is Nothing
            sr.Close()
        End Using
    Catch exc As System.Exception
    End Try
    mail.Close(OlInspectorClose.olDiscard)
    Return r
End Function

Private Function CheckForRedCategory(mail As MailItem) As Boolean ' errrrrr
    Dim b As Boolean = False
    Try
        If (mail.Categories.Equals("Red Category")) Then
            b = True

            mail.Close(OlInspectorClose.olDiscard)
            Return b
        Else
            b = False
        End If
    Catch exc As System.NullReferenceException
        b = False
        mail.Close(OlInspectorClose.olDiscard)
    End Try
    mail.Close(OlInspectorClose.olDiscard)
    Return b
End Function

0 个答案:

没有答案