Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I supress the Outlook warning while sending mail using macro in excel

Tags:

excel

vba

outlook

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

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?

like image 610
Anurag Rana Avatar asked Apr 23 '13 11:04

Anurag Rana


People also ask

How do I turn off Outlook warning emails?

In Outlook, select File, and then select Options. Select Trust Center, and then select Trust Center Settings. Select Programmatic Access. If we want to stop these warning permanently, select the Never warn me about suspicious activity (not recommended) option.

How do I stop triggers for display alert or error warning while running the macro?

DisplayAlerts = False: This is a property of the application object. See here we have called it using “.” operator just. This line disables all alerts of the closing file, overwriting, or opening an already open file.


1 Answers

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).

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

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.

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.

like image 93
Graham Anderson Avatar answered Sep 26 '22 12:09

Graham Anderson