【问题标题】:Sending emails from Excel using Outlook without security warning使用 Outlook 从 Excel 发送电子邮件而没有安全警告
【发布时间】:2014-01-28 08:58:32
【问题描述】:

我正在使用 Ron de Bruin 网站上的代码使用 Outlook 通过 Excel 发送电子邮件。我收到此安全警告“程序正在尝试代表您发送电子邮件”,要求我允许或拒绝。

如何避免此警告并直接发送电子邮件"

注意:我使用的是 Excel 2007。

代码如下:

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim cell As Range

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

Sheets("" & Sheet & "").Select
With Sheets("" & Sheet & "")
    strbody = ""
End With

On Error Resume Next
With OutMail
    .To = " email1@a.com"
    .CC = ""
    .BCC = ""
    .Subject = ""
    .Body = strbody
    .From = ""
    .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

' restore default application behavior
Application.AlertBeforeOverwriting = True
Application.DisplayAlerts = True
ActiveWindow.SelectedSheets.PrintOut Copies:=3, Collate:=True

【问题讨论】:

  • 查看类似问题的答案here

标签: vba excel outlook excel-2007


【解决方案1】:

除了评论中link 中描述的方法外,假设您是发件人“...要求我允许或拒绝”,如果您正在运行 Excel,则可以已经拥有 Outlook strong> 也在运行。

最简单的方法是:

Set OutApp = GetObject(, "Outlook.Application") 

【讨论】:

    【解决方案2】:

    几年前我在互联网的某个地方找到了下面的代码。它会自动为您回答“是”。

    Option Compare Database
        ' Declare Windows' API functions
        Private Declare Function RegisterWindowMessage _
                Lib "user32" Alias "RegisterWindowMessageA" _
                (ByVal lpString As String) As Long
    
         Private Declare Function FindWindow Lib "user32" _
                    Alias "FindWindowA" (ByVal lpClassName As Any, _
                    ByVal lpWindowName As Any) As Long
    
    
        Private Declare Function SendMessage Lib "user32" _
                Alias "SendMessageA" (ByVal hwnd As Long, _
                ByVal wMsg As Long, ByVal wParam As Long, _
                lParam As Any) As Long
    
    
        Function TurnAutoYesOn()
        Dim wnd As Long
        Dim uClickYes As Long
        Dim Res As Long
        uClickYes = RegisterWindowMessage("CLICKYES_SUSPEND_RESUME")
        wnd = FindWindow("EXCLICKYES_WND", 0&)
        Res = SendMessage(wnd, uClickYes, 1, 0)
    
        End Function
    
        Function TurnOffAutoYes()
        Dim wnd As Long
        Dim uClickYes As Long
        Dim Res As Long
        uClickYes = RegisterWindowMessage("CLICKYES_SUSPEND_RESUME")
        wnd = FindWindow("EXCLICKYES_WND", 0&)
        Res = SendMessage(wnd, uClickYes, 0, 0)
        End Function
    
    
        Function fEmailTest()
    
        TurnAutoYesOn  '*** Add this before your email has been sent
    
    
    
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)
        With MailOutLook
            .To = " <Receipient1@domain.com>;  <Receipient2@domain.com"
            .Subject = "Your Subject Here"
            .HTMLBody = "Your message body here"
            .Send
        End With
    
        TurnOffAutoYes '*** Add this after your email has been sent
    
        End Function
    

    【讨论】:

    • 是否需要这个 ClickYes 应用程序? contextmagic.com/express-clickyes
    • 不,不是。我的声明中受到了打击。选项比较数据库公共声明函数RegisterWindowMessage_Lib“user32”别名“RegisterWindowMessageA”_(ByVal lpString As String)只要公共声明函数FindWindow Lib“user32”_别名“FindWindowA”(ByVal lpClassName As Any,_ByVal lpWindowName As Any)只要公共声明函数SendMessage Lib“user32”_别名“SendMessageA”(ByVal hwnd As Long,_ByVal wMsg As Long,ByVal wParam As Long,_lParam As Any)只要
    • @JuliaGrant 这仍然给 Option Compare Database 一个编译错误,你有解决办法吗?
    • 您能否将这些声明也添加到您的答案here 中?
    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2018-05-07
    • 2010-11-06
    相关资源
    最近更新 更多