使用VBA将Outlook中的硬删除项重定向到已删除的项目

时间:2011-06-30 16:51:50

标签: vba outlook-vba

正如标题中所述,我正试图阻止Outlook中的硬件删除项目。我能够捕获BeforeItemMove事件中的操作。然后,用户可以选择是继续还是取消。如果他决定继续,则该项目应移至“已删除邮件”文件夹,不会永久删除。

我的第一个想法是通过将“取消”设置为“真”然后将项目移动到“已删除邮件”文件夹来取消删除操作。问题是,事件再次为移动操作触发,但是手动项目对象似乎以某种方式被破坏。我尝试在已删除的项目上设置UserProperty然后移动它。但是当我尝试阅读道具时,在event-sub的“第二次运行”中,我得到一个运行时错误,说无法找到该消息。

可以S.O.帮助

以下是涉及的两个事件处理程序:

    Private Sub oTasks_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As Folder, Cancel As Boolean)

    Dim shouldDelete As Boolean
    shouldDelete = False

    Dim hardDeletePerformed
    hardDeletePerformed = False


    If (MoveTo Is Nothing) Then
        shouldDelete = True
        hardDeletePerformed = True
    ElseIf (g_oNS.CompareEntryIDs(MoveTo.EntryID, oDeletedItems.EntryID)) Then
        shouldDelete = True
    End If

    Dim oTask As TaskItem
    Set oTask = Item




    If shouldDelete Then
        If (InStr(1, oTask.Subject, "frist", vbTextCompare)) Then
            Dim message As String
            message = "..."
            Dim res As VbMsgBoxResult

            res = MsgBox(message, vbOKOnly + vbCritical, "Compliance-Warnung!")
            Cancel = True
        Else
            Dim message2 As String
            message2 = "..."

            Dim res2 As VbMsgBoxResult

            res2 = MsgBox(message2, vbYesNo + vbCritical, "Compliance-Warnung!")
            If (res2 = vbYes) Then
                Cancel = False
                If hardDeletePerformed Then
                    oTask.Move oDeletedItems
                    Cancel = True
                End If
            Else
                Cancel = True
            End If
        End If
    End If
End Sub

    Private Sub oAppointments_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As Folder, Cancel As Boolean)


        If inProgress Then
            Cancel = True
            inProgress = False
        Else



        Dim shouldDelete As Boolean
        shouldDelete = False

        Dim hardDeletePerformed
        hardDeletePerformed = False


        If (MoveTo Is Nothing) Then
            shouldDelete = True
            hardDeletePerformed = True
        ElseIf (g_oNS.CompareEntryIDs(MoveTo.EntryID, oDeletedItems.EntryID)) Then
            shouldDelete = True
        End If

        Dim oAppointment As AppointmentItem
        Set oAppointment = Item


        If shouldDelete Then
            If (InStr(1, oAppointment.Subject, "frist", vbTextCompare)) Then
                Dim message As String
                message = "..."
                Dim res As VbMsgBoxResult

                res = MsgBox(message, vbOKOnly + vbCritical, "Compliance-Warnung!")
                Cancel = True
            Else
                Dim message2 As String
                message2 = "..."

                Dim res2 As VbMsgBoxResult

                res2 = MsgBox(message2, vbYesNo + vbCritical, "Compliance-Warnung!")
                If (res2 = vbYes) Then
                    Cancel = False
                    If hardDeletePerformed Then
                        inProgress = True
                        oAppointment.Move oDeletedItems
                        oAppointment.Save
                        'inProgress = False
                        Cancel = True
                    End If
                Else
                    Cancel = True
                End If
            End If
        End If

        End If

    End Sub

奇怪的是,oTasks的第一个eventhandler完全按照我想要的方式工作。该项目将移至已删除的项目,并且只会调用一次事件处理程序。 oAppointments的第二个将被调用两次没有Tims对inProgress-if子句的建议...而且真的很奇怪的是,在第二个事件处理程序中,项目被移动到草稿而不是删除项目,但是oDeletedItems-Object没有改变...任何想法?

PS:我讨厌VBA!

2 个答案:

答案 0 :(得分:1)

我打算建议你使用

Application.EnableEvents=False

在移动itam之前暂时禁用事件,但在检查时似乎OutLook VBA中没有这样的东西。另一种方法是使用静态变量来跳过Move事件。

Intested pseudocode:

Sub SomeEventHandler()

   Static inProcess as Boolean

   If inProcess then Exit Sub

   If IsHardDelete then
      inProcess = True
      'move item
      inProcess = False
   End If

End Sub

答案 1 :(得分:0)

我认为调用oAppointment.Save会将AppointmentItem保存到当前文件夹,大概是Drafts。之前对oAppointment.Move oDeletedItems的调用不会更改当前文件夹。

您确定需要保存oAppointment,因为您没有将oTask保存在其他事件处理程序中吗?