MailItem.BeforeDelete 事件 (Outlook)
在删除项目(父对象的一个实例)前发生。
语法
表达式。BeforeDelete (Item, Cancel)
expression 表示 MailItem 对象的变量。
参数
名称 | 必需/可选 | 数据类型 | 说明 |
---|---|---|---|
Item | 必需 | 对象 | 正在删除的项目。 |
Cancel | 必需 | Boolean | 假 的事件发生时。 如果事件过程将此参数设置为 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
另请参阅
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈