【问题标题】:Stop Outlook VBA from sending mail within ItemSend() when an error event has occurred发生错误事件时,停止 Outlook VBA 在 ItemSend() 中发送邮件
【发布时间】:2018-11-14 01:41:52
【问题描述】:

当运行时在 ItemSend() 事件中发生错误时,将发送 Outlook 邮件项。在代码中放置Cancel = True 甚至不能阻止这种情况的发生。这是 VBA 固有的缺陷吗?

我将如何解决这个问题?欢迎任何想法。

Public WithEvents myApp As Outlook.Application

Sub Initialize_handler()
    Set myApp = Application
End Sub

Private Sub myApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler_myApp_ItemSend
    Cancel = True

    ' Do something erroneous

    Exit Sub

ErrorHandler_myApp_ItemSend:
    Cancel = True
    MsgBox "Error: " Err.Description
    Err.Clear
End Sub

【问题讨论】:

  • 在两个 Cancel = True 行上放置一个断点,然后运行您的代码。代码是否到达?
  • 如果所有这些代码都在 ThisOutlookSession 中,您是否可以通过删除前四行并更改为 Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 来编辑问题以消除不必要的复杂情况。你会输入Err.Raise,这样潜在的受访者就不会引入试图制造错误的错误。
  • nitro@ 感谢您的提示。

标签: vba events outlook onerror


【解决方案1】:

我没有解释为什么关闭检查器时没有发送邮件。

Option Explicit

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

    Dim objInsp As Inspector

    On Error GoTo ErrorHandler_Application_ItemSend

    ' Do something erroneous
    Err.Raise 1

    Exit Sub

ErrorHandler_Application_ItemSend:

    Cancel = True
    MsgBox "Error: " & Err.Description

    Set objInsp = Item.GetInspector

    ' Look for the mail in the drafts folder
    objInsp.Close olSave

End Sub

【讨论】:

  • 感谢您提供代码。令人难以置信,因为突然之间代码似乎又可以正常工作了。我不能确定,但​​它可能与所问的问题无关。我将不得不等待,看看问题是否会再次出现。
【解决方案2】:

你不能使用 On Error GoTo 来获取错误事件,你可以尝试使用 If 语句。例如:

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

On Error Resume Next

If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
      prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
         Cancel = True
       End If
End If

End Sub

更多信息,请参考此链接:

Warn Before Sending Messages to the Wrong Email Address

Application.ItemSend Event (Outlook)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多