使用Outlook 2016 for Windows(10)。Outlook VBA - BeforeDelete不起作用
我想寫一些VBA來自動將我刪除的電子郵件複製到一個單獨的文件夾(或者也許只是複製覆蓋「刪除」)。
在我得到這一點之前,pre-req需要設置一個簡單的VBA腳本來捕獲刪除事件。
我在MSDN上看了一下,發現下面的代碼插入到「ThisOutlookSession」對象中。
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
但是,這不會運行。當我去刪除一個項目時(通過點擊收件箱中的「del」鍵或打開項目並點擊功能區上的「刪除」),項目移動到已刪除郵件文件夾,但我沒有看到消息框。這似乎相對簡單,我不確定我錯過了什麼。
什麼導致DeleteMail子文件運行?如果沒有,myItem從不初始化。 –