共用方式為


Outlook) (MailItem.BeforeDelete 事件

會在刪除項目 (父物件的執行個體) 之前發生。

語法

運算式BeforeDelete (ItemCancel)

表達 代表 MailItem 物件的 變數。

參數

名稱 必要/選用 資料類型 描述
項目 必要 Object 要刪除的項目。
Cancel 必要 布林值 False 事件發生時。 如果事件程序將此引數設定為 True,則無法完成作業,而且無法刪除項目。

註解

若要在透過動作刪除電子郵件訊息、通訊群組清單、記錄項目、工作、連絡人或貼文時引發此事件,必須開啟偵測器。

每次刪除項目時就會發生此事件。

範例

下列 Visual Basic for Applications (VBA) 範例會提示使用者是否要刪除目前已開啟的項目。 若要執行此範例,您必須有可刪除的開啟電子郵件專案。 如果您按一下 [否],則不會刪除此項目。 如果取消此事件,Microsoft Outlook 會顯示錯誤訊息。 因此,您必須在程式碼中擷取此事件。 下面顯示擷取事件的其中一個方法。 範例程式碼必須放在類別模組中,例如 ThisOutlookSession ,而且 DeleteMail() 應該先呼叫程式,Outlook 才能呼叫事件程序。

Public WithEvents myItem As Outlook.MailItem 
 
 
 
Public Sub DeleteMail() 
 
 Const strCancelEvent = "Application-defined or object-defined error" 
 
 On Error GoTo ErrHandler 
 
 Set myItem = Application.ActiveInspector.CurrentItem 
 
 myItem.Delete 
 
 Exit Sub 
 
 
 
ErrHandler: 
 
 MsgBox Err.Description 
 
 If Err.Description = strCancelEvent Then 
 
 MsgBox "The event was cancelled." 
 
 End If 
 
 'If you want to execute the next instruction 
 
 Resume Next 
 
 'Otherwise it will finish here 
 
End Sub 
 
 
 
Private Sub myItem_BeforeDelete(ByVal Item As Object, Cancel As Boolean) 
 
 'Prompts the user before deleting an item 
 
 Dim strPrompt As String 
 
 
 
 'Prompt the user for a response 
 
 strPrompt = "Are you sure you want to delete the item?" 
 
 If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbNo Then 
 
 'Don't delete the item 
 
 Cancel = True 
 
 End If 
 
End Sub

另請參閱

MailItem 物件

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應