我正在尝试使用 excel 中的宏发送电子邮件。
但是,当我运行此代码时,我的邮件客户端(即 MS Outlook)会显示类似于以下内容的弹出警告
Someone is tying to send mail on behalf of you. select yes or no
有没有办法使用vba来抑制该警告,以便发送电子邮件没有任何问题?
我知道的最好的方法是创建一个 Outlook 应用程序项目,创建消息,显示消息并使用 sendkeys 发送消息(相当于键入 alt s)。
缺点是 sendkeys 方法可能有点错误。为了使它更健壮,我得到了邮件项目的检查器,即它所在的窗口,并在调用 sendkeys 之前立即激活它。代码如下所示:
Dim olApp As outlook.Application
Dim objNS As Outlook.Namespace
Dim objMail As Outlook.MailItem
Dim objSentItems As Outlook.MAPIFolder
Dim myInspector As Outlook.Inspector
'Check whether outlook is open, if it is use get object, if not use create object
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If
Set objNS = olApp.GetNamespace("MAPI")
objNS.Logon
'Prepare the mail object
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = <insert recipients name as string>
.Subject = <insert subject as string>
.Body = <insert message as string>
.Display
End With
'Give outlook some time to display the message
Application.Wait (Now + TimeValue("0:00:05"))
'Get a reference the inspector obj (the window the mail item is displayed in)
Set myInspector = objMail.GetInspector
'Activate the window that the mail item is in and use sendkeys to send the message
myInspector.Activate
SendKeys "%s", True
然后我通常有代码检查已发送文件夹中的项目数量是否增加,如果没有,我让应用程序再次等待并重复最后两行代码并重新检查已发送文件夹中的消息数量是否增加。该代码最多执行 5 次。第 5 次后会出现一个消息框,警告消息可能尚未发送。
虽然我曾经在我们的系统特别慢时看到警告消息,但我从未发现这种方法无法从 excel 发送消息,但经过调查发现消息已发送。
您需要使用 Redemption DLL 来禁用此警告...
下载 http://www.dimastr.com/redemption
我创建了一种在机器上自动安装此 DLL 的方法,您可以尝试...
http://www.officevb.com/2011/02/copiando-e-registrando-componentes-na.html
添加到 Julia Grant 的回答和回答 dsauce
直接使用 Julia 代码时出现错误RegisterWindowMessage
这应该通过
在声明部分替换Private Declare Function
为Declare PtrSafe Function
Option Compare Database
' Declare Windows' API functions
Declare PtrSafe Function RegisterWindowMessage _
Lib "user32" Alias "RegisterWindowMessageA" _
(ByVal lpString As String) As Long
Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long
Declare PtrSafe 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
我知道线程很旧,但它可能会帮助某人
几年前,我在互联网上的某个地方找到了下面的代码。它会自动为您回答“是”。
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
我检查了很多方法,但这是最好的。
您只需要创建 Excel 的最小化窗口代码,然后将其最大化以便工作,并在脚本点击“发送”后添加一点延迟。
只要确保在运行时不要触摸任何鼠标或键盘。我每天可以自动发送 25 多封没有错误的电子邮件。
'Excel Minimized
ActiveWindow.WindowState = xlMinimized
'Outlook Email Shown
.Display
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"
Application.Wait (Now + TimeValue("0:00:01"))
'Excel Maximized
ActiveWindow.WindowState = xlMaximized
此 Outlook VBA 将加载一个带有存储为记录的电子邮件的 excel 文件并发送所有这些文件。
Option Explicit
Private Const xlUp As Long = -4162
Sub SendEmailsFromExcel()
Dim xlApp As Object
Dim isEmailTo As String ' Col A
Dim isSubject As String ' Col B
Dim isMessage As String ' Col C
Dim i As Integer
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
Dim emailsMatrix As Variant
Dim objWB As Object
Dim objWs As Object
Dim FileStr As String
FileStr = "C:\Users\...\Documents\EmailsInExcel.xlsx"
Set xlApp = CreateObject("excel.application")
With xlApp
.EnableEvents = False
.DisplayAlerts = False
End With
Set objWB = xlApp.Workbooks.Open(FileStr)
Set objWs = objWB.Sheets(1)
' Matrix load: A - Email Address, B - Subject, C - Body
emailsMatrix = objWs.Range("A1:C" & xlApp.Cells(objWs.Rows.Count, "A").End(xlUp).Row)
objWB.Close
Set objWB = Nothing
xlApp.Quit
Set xlApp = Nothing
' Done getting Excel emails file.
For i = 1 To UBound(emailsMatrix)
isEmailTo = emailsMatrix(i, 1)
isSubject = emailsMatrix(i, 2)
isMessage = emailsMatrix(i, 3)
objMsg.Recipients.Add isEmailTo
objMsg.Subject = isSubject
objMsg.Body = isMessage
objMsg.Send
Next i
End Sub
由于宏未由受信任的发布者签名,该窗口开始弹出。此列表在您的 Outlook 设置中。您必须对宏进行签名并将签名者输入到您信任的发布者列表中。或者全局允许未签名的宏。
几个选项:
有关讨论和可用选项列表,请参见http://www.outlookcode.com/article.aspx?id=52 。