Vba 在没有安全警告的情况下使用Outlook从Excel发送电子邮件

Vba 在没有安全警告的情况下使用Outlook从Excel发送电子邮件,vba,excel,outlook,excel-2007,Vba,Excel,Outlook,Excel 2007,我使用Ron de Bruin网站上的代码使用Outlook通过Excel发送电子邮件。我收到此安全警告“一个程序正试图代表您发送电子邮件”,要求我允许或拒绝 如何避免此警告并直接发送电子邮件“ 注意:我使用的是Excel2007 代码如下: Dim OutApp As Object Dim OutMail As Object Dim strbody As String Dim cell As Range Set OutApp = CreateObject("Outlook.Applicati

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

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

注意:我使用的是Excel2007

代码如下:

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

除了评论中描述的方法外,假设您是发件人“…要求我允许或拒绝”,如果您运行Excel,您也可以让Outlook已经运行

最简单的方法是:

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

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

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
选项比较数据库
“声明Windows”API函数
私有声明函数RegisterWindowMessage_
Lib“user32”别名“RegisterWindowMessageA”_
(ByVal lpString作为字符串)一样长
私有声明函数findwindowlib“user32”_
别名“FindWindowA”(ByVal lpClassName,如有)_
ByVal lpWindowName(如有)尽可能长
私有声明函数SendMessage Lib“user32”_
别名“SendMessageA”(ByVal hwnd,长度为_
ByVal wMsg尽可能长,ByVal wParam尽可能长_
lpram(如有)一样长
函数TurnAutoYesOn()
暗淡无光
是的,很长
暗淡如长
uClickYes=RegisterWindowMessage(“单击是、暂停、恢复”)
wnd=FindWindow(“排他性”,0&)
Res=SendMessage(wnd,uClickYes,1,0)
端函数
功能关闭Autoyes()
暗淡无光
是的,很长
暗淡如长
uClickYes=RegisterWindowMessage(“单击是、暂停、恢复”)
wnd=FindWindow(“排他性”,0&)
Res=SendMessage(wnd,uClickYes,0,0)
端函数
函数fEmailTest()
TurnAutoYesOn'***在发送电子邮件之前添加此项
设置appOutLook=CreateObject(“Outlook.Application”)
设置mailookout=appOutLook.CreateItem(olMailItem)
使用MailOutLook

.To=“;查看类似问题的答案是否需要此ClickYes应用程序?不,不是。我的声明受到了打击。选项比较数据库公共声明函数RegisterWindowMessage ulib“user32”别名“RegisterWindowMessageA”(ByVal lpString As String)和公共声明函数findwindowlib“user32”别名“FindWindowA”(ByVal lpClassName As Any,val lpWindowName As Any)只要公开声明函数SendMessage Lib“user32”别名“SendMessage a”(ByVal hwnd为Long,val wMsg为Long,ByVal wParam为Long,lParam为Any)为Long@JuliaGrant这仍然会导致Option Compare数据库的编译错误,你对此有解决办法吗?你能把这些声明也添加到你的答案中吗?