vba 在 Outlook 中向外部域发送电子邮件之前发出警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17757373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:12:06  来源:igfitidea点击:

Warn before sending emails to external domains in Outlook

emailvbaoutlookoutlook-vba

提问by ojhawkins

How can you get Outlookto warn you if you are about to send and email to an external domain?

Outlook如果您要向外部域发送和发送电子邮件,如何警告您?

Sending large amounts of emails everyday it is always possible to incorrectly send one to the wrong person. This is especially a problem when they are clients or people outside of your company.

每天发送大量电子邮件总是有可能将一封错误地发送给错误的人。当他们是您公司以外的客户或人员时,这尤其成问题。

Using Alt + Enterto quickly send emails after typing them for me is often the cause as I do not check the recipients thoroughly.

Alt + Enter在为我输入电子邮件后使用快速发送电子邮件通常是原因,因为我没有彻底检查收件人。

I have found numerous implementations which were not great so I thought I would share mine below...

我发现了许多不太好的实现,所以我想我会在下面分享我的......

回答by matho

Thanks ojhhawkins for the code above - really useful. I've done a simple iteration to include a list of the external email addresses in the MsgBox text.

感谢 ojhhawkins 提供上面的代码 - 非常有用。我已经完成了一个简单的迭代,以在 MsgBox 文本中包含外部电子邮件地址列表。

Word of caution - I've noticed that the warning doesn't appear when you use the Send As Email Attachment in other programmes, eg Excel, Adobe Reader etc. As nitonpointed out:

注意事项 - 我注意到当您在其他程序(例如 Excel、Adobe Reader 等)中使用 Send As Email Attachment 时不会出现警告。正如niton指出的那样:

Re:Send As Email Attachment in other programmes. Described in notes here outlookcode.com/d/code/setsavefolder.htm "... does not work on messages created with File | Send commands in Office programs or similar commands in Windows Explorer or other programs. Those commands invoke Simple MAPI, which bypasses Outlook functionality."

回复:在其他程序中作为电子邮件附件发送。在此处的注释中描述了 Outlookcode.com/d/code/setsavefolder.htm "... 不适用于使用 File | Send 命令在 Office 程序中创建的消息或在 Windows 资源管理器或其他程序中的类似命令。这些命令调用简单 MAPI,它绕过 Outlook 功能。”

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim recips As Outlook.Recipients
    Dim recip As Outlook.Recipient
    Dim pa As Outlook.PropertyAccessor
    Dim prompt As String
    Dim strMsg As String

    Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

    Set recips = Item.Recipients
    For Each recip In recips
        Set pa = recip.PropertyAccessor
        If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "@example.com") = 0 Then
            strMsg = strMsg & "   " & pa.GetProperty(PR_SMTP_ADDRESS) & vbNewLine
        End If
    Next

    If strMsg <> "" Then
        prompt = "This email will be sent outside of example.com to:" & vbNewLine & strMsg & "Do you want to proceed?"
        If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
            Cancel = True
        End If
    End If
End Sub

To actually add this code to your Outlook application:

将此代码实际添加到您的 Outlook 应用程序:

  • If you can't see the Developer tab in the ribbon bar, go to File/Options, choose Customise Ribbonon the left, and tick Developeron the right.
  • From the Developertab choose Visual Basic.
  • Expand Project1, Microsoft Outlook Objects, and double-click ThisOutlookSession (top left).
  • Paste the code above into the module.
  • Replace the "example.com" in the copied code to your domain.
  • Close the VBA editor and save changes to the module.
  • On the Developertab click Macro Security, and change the level to Notifications for all macrosor lower.
  • Restart Outlook. (The code above will not initialise otherwise.)
  • 如果在功能区栏中看不到“开发工具”选项卡,请转到“文件/选项”,在左侧选择“自定义功能区”,然后在右侧勾选“开发工具”。
  • 开发人员选项卡中选择Visual Basic
  • 展开 Project1、Microsoft Outlook Objects,然后双击 ThisOutlookSession(左上角)。
  • 将上面的代码粘贴到模块中。
  • 将复制的代码中的“example.com”替换为您的域。
  • 关闭 VBA 编辑器并保存对模块的更改。
  • 在“开发人员”选项卡上单击“宏安全”,然后将级别更改为“所有宏的通知”或更低级别。
  • 重新启动 Outlook。(否则上面的代码不会初始化。)

回答by ojhawkins

  1. Add the below code to the Application_ItemSendevent in Outlook& change the domain to your own

  2. Change the Macro Securityto either (Notifcations for all macrosor Enable all macros)

  1. 将以下代码添加到Outlook 中Application_ItemSend事件并将域更改为您自己的域

  2. 更改Macro Security为(所有宏的通知启用所有宏

This will provide you with a warning before sending if 1 or more of your TO,CCor BCCaddress is not in your domain (eg below @mycompany.com.au)

这将为您提供一个警告,如果发送1个或多个您之前TOCCBCC地址不在您的域(例如低于@mycompany.com.au

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim recips As Outlook.Recipients
    Dim recip As Outlook.Recipient
    Dim pa As Outlook.PropertyAccessor
    Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
    Set recips = Item.Recipients
    For Each recip In recips
        Set pa = recip.PropertyAccessor
        If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "@mycompany.com.au") = 0 Then
            If MsgBox("Send mail to external domain?", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
                Cancel = True
                Exit Sub
            Else
                Exit Sub
            End If
        End If
    Next
End Sub

回答by Nic

I found two add-ins for Outlook that does the same if you don't want to use VBA,

如果您不想使用 VBA,我发现有两个 Outlook 加载项可以执行相同的操作,