在outlook重启后宏不工作

时间:2017-10-03 09:54:08

标签: vba outlook

我有一个代码,用于检查特定字符串的主题和电子邮件,并通过YES / NO选项通知用户。

当我重新启动outlook时,我需要手动运行MACRO才能使其正常工作。

我尝试了下面的代码,但我仍然需要手动运行它。

Public WithEvents PasswordCheck As Outlook.Application
Public Sub Initialize_handler()
   Set PasswordCheck = Outlook.Application
End Sub

Private Sub PasswordCheck_ItemSend(ByVal Item As Object, Cancel As Boolean)
  Dim strBody As String
  Dim strSubject As String
  strSubject = Item.Subject
  strBody = Item.Body
UCasePasswd = UCase("Test_123")
prompt = "Are you sure you want to send this email? It contains  Password: "
If InStr(1, UCase(strSubject), UCasePasswd) > 0 Or _
InStr(1, UCase(strBody), UCasePasswd) > 0 Then
If MsgBox(prompt, vbYesNo + vbQuestion, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
End Sub

Private Sub PasswordCheck_Startup()
Initialize_handler
End Sub

1 个答案:

答案 0 :(得分:0)

更改您的程序名称

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

应该是

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

并存储在ThisOutlookSession模块中。

您还应将Option Explicit放在模块的顶部,并将UCasePasswdprompt声明为字符串变量。

测试完成后,无需使用Application_Startup()例程调用此代码,因为它是一个内置事件。

此外,我不认为您可以Application_Startup重新引用PasswordCheck_Startup,就像我从未见过Private Sub Workbook_Open()更改为Excel中的任何其他内容一样。

修改 ThisOutlookSession中的代码完美无缺 - 它是模块中唯一的代码。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strBody As String
    Dim strSubject As String
    Dim UCasePasswd As String
    Dim prompt As String

    strSubject = Item.Subject
    strBody = Item.Body
    UCasePasswd = "TEST_123" 'Rather than use UCASE, just write it in upper case.
    prompt = "Are you sure you want to send this email? It contains Password: "
    If InStr(UCase(strSubject), UCasePasswd) > 0 Or InStr(UCase(strBody), UCasePasswd) > 0 Then
        If MsgBox(prompt, vbYesNo + vbQuestion, "Check for Subject") = vbNo Then
            Cancel = True
        End If
    End If
End Sub