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

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.

Tags:

Excel

Vba

Outlook