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



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

我添加了一行(在代码中标记),它抛出一个错误" the Item。关闭命令不能在项目中执行。发送事件"

这是可以理解的,但我怎么才能克服这个?

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虽然…

最新更新