2011-06-30 10 views
0

タイトルで述べたように、Outlookでアイテムを削除するのを防止しようとしています。私はBeforeItemMoveイベントで操作をキャッチすることができます。その後、ユーザは、続行するかキャンセルするかの選択肢が与えられる。彼が進めることを決定した場合、そのアイテムは「削除済みアイテム」フォルダに移動さ​​れ、永久に削除されません。Outlookでハード削除されたアイテムをVBAを使用して削除済みアイテムにリダイレクトする

私の最初のアイデアは、キャンセル操作をTrueに設定してアイテムを[削除済みアイテム]フォルダに移動することで、削除操作をキャンセルすることでした。問題は、移動操作のために再びイベントが発生することですが、手渡されたアイテムオブジェクトは何とか壊れているようです。私は、削除されたアイテムにUserPropertyを設定して、それを移動しようとしました。しかし、私は小道具を読むしようとするイベントサブの "第二の実行"で、私は、メッセージが見つかりませんでしたランタイムエラーを取得します。

Can S.O.助けて?ここで

が関与する2つのイベントハンドラです:

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はまさに私はそれがしたいように動作します。アイテムは削除されたアイテムに移動され、イベントハンドラは一度だけ呼び出されます。 2番目のoAppointmentsは、Timの提案なしにinProgress-if-clause ...を呼び出さなくてはならず、2番目のイベントハンドラでは、アイテムはDraftsに移動され、Deleted ItemsではなくoDeletedItems-Objectその間に変更されていない...任意のアイデア?

PS:私はVBAが嫌です!

+0

常に実際のコードを表示するのに役立ちます... –

答えて

1

私はあなたがITAMを移動する前に、一時的にイベントを無効にする

Application.EnableEvents=False 

を使用することをお勧めするつもりだったが、チェックの上、Outlook VBAにはそのようなことはありませんようです。代わりに、静的変数を使用してMoveイベントをスキップできるようにすることもできます。

Intested擬似コード:

Sub SomeEventHandler() 

    Static inProcess as Boolean 

    If inProcess then Exit Sub 

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

End Sub 
+0

ありがとうございました。私はinProgress = falseを最初のif節に移動しなければならなかった。新しいイベントを開始する前に、イベントハンドラがサブ全体を完了したようです。それでもなお問題があります。何らかの理由で、項目が削除された項目ではなく、下書きに移動されます。move-methods引数が削除された項目を戻しています...別のイベントハンドラは、inProgress-partを一度呼び出すだけで、 ..働いているのはTaskItemsで、失敗したものはAppointmentItemsです。どのように考えられるか? – Tobi

+0

元の質問に2つのイベントハンドラーを追加しました – Tobi

+0

申し訳ありません - Outlookプログラミングは私のものではありません:予定がDraftsで終わった理由についての提案はできません –

0

私はoAppointment.Saveを呼び出すことおそらくDraftsある現在のフォルダにAppointmentItemを救うことだと思います。以前のoAppointment.Move oDeletedItemsの呼び出しでは、現在のフォルダーは変更されません。

他のイベントハンドラにoTaskを保存しないので、oAppointmentを保存する必要がありますか?

関連する問題