awel20 wrote:Code: Select all
#Persistent
olMailItem := 0
ol := ComObjCreate("Outlook.Application")
newMail := ol.CreateItem(olMailItem)
newMail.Subject := "Hello World!"
newMail.Display
; Connect to newMail events
ComObjConnect(newMail, new MailItemEvents)
return
class MailItemEvents
{
; MailItem.Send Event (Outlook)
; https://docs.microsoft.com/en-us/office/vba/api/Outlook.MailItem.Send(even)
Send(Cancel, MailItem)
{
MsgBox % "Sending: " MailItem.Subject
; Disconnect from MailItem events
ComObjConnect(MailItem)
ExitApp
}
}
The MailItem.Send Event only links to a specific MailItem which is very limiting. If you want a more global approach then you can use the application event ItemSend. This is triggered any time the application sends an email not just when a specific email is sent.
Code: Select all
#Persistent
olApp := ComObjActive("Outlook.Application")
ComObjConnect(olApp, "EventApp_")
EventApp_ItemSend(olItem, Cancel)
{
if (olItem.SendUsingAccount.DisplayName = "[email protected]")
{
MsgBox % olItem.Subject "`nYou are not authorized to send emails from: [email protected]"
return ComObject(0xB,0) ; VT_Bool False = Cancels Send with Email staying open
}
if InStr(olItem.Body, "stupid")
{
MsgBox The new company policy is to NOT call our customers "stupid".`nPlease rephrase your email.
return ComObject(0xB,0) ; VT_Bool False = Cancels Send with Email staying open
}
}
Just an aside, I us a script like this on multiple computers within an office to check for stuff before an email is sent but I found it better to use an VBA script within Outlook so that the script is always running when Outlook is running. It does require some know how to get the VBA script to run automatically when Outlook starts without any security warnings. It has been very clean and reliable, and pretty dummy-proof once setup in Outlook.
FG