如何在 Excel 中使用宏发送邮件时抑制 Outlook 警告 [英] How can I supress the Outlook warning while sending mail using macro in excel

查看:31
本文介绍了如何在 Excel 中使用宏发送邮件时抑制 Outlook 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 excel 中的宏发送电子邮件.

I am trying to send an email using macro in excel.

但是当我运行此代码时,我的邮件客户端,即 MS Outlook 显示类似于
的弹出警告有人试图代表您发送邮件.选择是或否

But when I run this code my mail client i.e. MS Outlook shows a pop up warning similar to
Someone is tying to send mail on behalf of you. select yes or no

有什么方法可以使用 来抑制那个警告所以应该可以毫无问题地发送电子邮件?

Is there any way using vba to suppress that warning so the email should be sent without any problem?

推荐答案

我所知道的最好的方法是创建 Outlook 应用程序项,创建消息,显示消息并使用 sendkeys 发送消息(相当于键入 alt s).

The best way I know is to create an outlook application item, create the message, display the message and use sendkeys to send the message (equivelent of typing alt s).

缺点是 sendkeys 方法可能有点错误.为了使它更健壮,我得到了邮件项目的检查员,即它所在的窗口,并在调用 sendkeys 之前立即激活它.代码如下所示:

The drawback is that the sendkeys method can be a bit buggy. To make it more robust I get the inspector for the mail item i.e. the window it is in and activate it immediately prior to the call to sendkeys. The code is shown below:

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 次后,将出现一个消息框,警告消息可能尚未发送.

I normally then have code to check that the number of items in the sent folder has increased and if not I get the application wait again and repeat the last 2 lines of code and recheck that the number of messages in the sent folder has increased. The code does this upto 5 times. After the 5th time a message box comes up warning that the message may not have been sent.

我从来没有发现这种方法在从excel发送消息时失败,尽管我曾经在我们的系统特别慢时看到警告消息,经调查发现消息已发送.

I have never found this method to fail in sending a message from excel though I once saw the warning message when our system was particularly slow, on investigation it turned out that the message had been sent.

这篇关于如何在 Excel 中使用宏发送邮件时抑制 Outlook 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆