首页 > 解决方案 > 如何避免对组织外的收件人发出警告?

问题描述

我正在编写一个 VBA Excel 脚本,以便将带有 Outlook 的电子邮件发送给不同的收件人,包括我组织之外的收件人。
我遵循了Ron DeBrujin的指南,它适用于与我的帐户在同一组织中的收件人,例如设置为“linello@mycorporation.com”。

Sub Mail_small_Text_Change_Account()
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim OutAccount As Outlook.Account
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    Set OutAccount = OutApp.Session.Accounts("linello@mycorporation.com")
    
    strbody = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"

    On Error Resume Next
    With OutMail
        .To = "anotheruser@anothercorporation.com"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = strbody
        .SendUsingAccount = OutAccount
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    Set OutAccount = Nothing
End Sub

如果一个或多个收件人属于不同的组织,Outlook 会提示警告。

“组织外的收件人”

它要求用户明确选择要发送消息的收件人,然后按 OK。

我尝试通过以下方式暂时禁用警报和警告:

Application.DisplayAlerts = False
Application.EnableEvents = False
With OutMail
    .To = "anotheruser@anothercorporation.com"
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .Body = strbody
    .SendUsingAccount = OutAccount
    .Send
End With
Application.DisplayAlerts = True
Application.EnableEvents = True

提示消息框仍然出现,要求我确认是否打算在组织外部发送电子邮件。
鉴于我需要发送邮件的大量地址,这种手动操作非常烦人且不需要。

VBA中有没有办法避免这个警告框?

标签: excelvbaoutlook

解决方案


推荐阅读