通过 VBA 在 Outlook 中取消项目发送事件后关闭发送邮件 Window

Closing Send Mail Window after Canceling Item Send event within Outlook via VBA

我正在编写一个 VBA 宏来防止向指定的电子邮件地址发送电子邮件。它是一个在 ThisOutlookSession 下运行的 Outlook 宏。代码运行正常,但问题是我无法关闭 Send Mail window。

我添加了一行(在代码中标记),它抛出一个错误 "Item.Close 命令无法在 Item.Send 事件期间执行"

这是可以理解的,但我该如何克服呢?

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

 If Item.To = "some@domain.com" Then
     Prompt$ = "Are you sure you want to send this message?"
   If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check before Sending") = vbNo Then
    Cancel = True

    Item.Close olDiscard          ' <<< ERROR HERE 

  End If
End If

End Sub

您关闭了 Item Inspector.

,而不是在发送事件仍为 运行 时关闭无法完成的项目本身

见下文:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objInsp As Inspector
Dim Strmsg As String
Set objInsp = Item.GetInspector
If Item.To = "testmail@gmail.com" Then
    Strmsg = "Are you sure you want to send this message?"
    If MsgBox(Strmsg, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check before Sending") = vbNo Then
        Cancel = True
        objInsp.Close 1
    End If
End If
End Sub

在 VBA 以外的语言中,您可以使用计时器 - 在 ItemSend 事件中启用计时器,当计时器事件触发时(届时您将离开 ItemSend 事件处理程序),禁用时间并关闭检查器。

我认为您不能在 Outlook 中使用计时器 VBA 不过...