'Outlook security prompt: Sendkeys does not have the right focus
I want to automate some basic operations in Outlook 2014, so I create a macro in VBA. But I need to send an email if any error occur during the execution of the script, and I want to bypass the confirmation message of Outlook (the pop up which appeared when you want to send a mail with a macro).
Private Sub sendEmailError()
Dim Email As Outlook.Application
Dim EmailMsg As Outlook.MailItem
Dim Dest As Outlook.Recipient
Set Email = CreateObject("Outlook.Application")
Set EmailMsg = Email.CreateItem(olMailItem)
Set Dest = EmailMsg.Recipients.Add("[email protected]")
EmailMsg.Subject = MessageSubject
EmailMsg.Body = MessageBody
EmailMsg.Display
SendKeys "^{ENTER}", True
End Sub
I thought "EmailMsg.Display" would give the focus to the message box and then, thanks to the sendkeys, I could execute the shortcut to send the email. But the focus is not given to the message box, and the control+key shortcut is done in my script after the end of the macro...
Thank you for your help !
Solution 1:[1]
You might be able to override the prompt, but kind of by definition you're not supposed to, and my guess is that Microsoft's probably put some security measures to prevent you from doing so. The prompt is meant to make sure that the user is OK with a macro messing with their emails, so it defeats the point to allow macros to just click 'OK' on behalf of the user.
What you can do instead is go into the Outlook Trust Center, and on the Programmatic Access tab, set it to 'Never warn me about suspicious activity'. This will remove the prompt permanently.
Solution 2:[2]
I know its not what you aks for, but
EmailMsg.Send
instead of
EmailMsg.Display
would send it right away
Solution 3:[3]
With the extra warning Message, it will look like this:
Private Sub sendEmailError()
Dim Email As Outlook.Application
Dim EmailMsg As Outlook.MailItem
Dim Dest As Outlook.Recipient
Set Email = CreateObject("Outlook.Application")
Set EmailMsg = Email.CreateItem(olMailItem)
Set Dest = EmailMsg.Recipients.Add("[email protected]")
EmailMsg.Subject = MessageSubject
EmailMsg.Body = MessageBody
EmailMsg.Send
SendKeys " "
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Josh Eller |
Solution 2 | Ockenheimer |
Solution 3 | Ockenheimer |