trouble implementing ItemSend event in Outlook Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thisiswill
Posts: 5
Joined: 04 Aug 2022, 08:08

trouble implementing ItemSend event in Outlook

Post by thisiswill » 04 Aug 2022, 09:24

Hi!

Long time lurker here, I've been able to solve problems by searching posts on the forum in the past but now I'm stuck !

I'm trying to write a script which would monitoring when a user sends a new email through Outlook app, and get those specific informations

- recipient e-mail address
- subject

So far, I have this code on a timer :

Code: Select all

IfWinActive, Message (HTML) ; only way I have found to see when a user is redacting a new email
	{
	active_window_id := WinExist("A")
	address:=
	subject:=
	loop ; loops continuously, until the user has entered the recipient's email address
		{
		IfWinNotExist, ahk_id %active_window_id%
			return
		olApp := ComObjActive("Outlook.Application")
		olEmail := olApp.ActiveWindow.CurrentItem
		for olRecipient in olEmail.Recipients
		address:=olRecipient.Address
		subject:=olEmail.subject
		if address=
			{
			sleep 500
			continue
			}
		else 
			break
		}
	}		
As you can see, I rely on ''IfWinActive'' to detect when a user is writing a new email, then waiting for the window to close (assuming the email has been sent)
However, this has proven unreliable, since opening a received email will trigger the same behavior by the script.

I am wondering if someone could help me to monitor when a user sends an email with ItemSend event https://docs.microsoft.com/en-us/office/vba/api/outlook.application.itemsend, or any other reliable way for that matter.

Thank you

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: trouble implementing ItemSend event in Outlook

Post by RussF » 04 Aug 2022, 10:03

Big Brother?

thisiswill
Posts: 5
Joined: 04 Aug 2022, 08:08

Re: trouble implementing ItemSend event in Outlook

Post by thisiswill » 04 Aug 2022, 10:09

haha not even.
We have to take notes at work when emailing customers, and I'm trying to automate part of it.

thisiswill
Posts: 5
Joined: 04 Aug 2022, 08:08

Re: trouble implementing ItemSend event in Outlook

Post by thisiswill » 08 Aug 2022, 10:35

Hello.

posting this in case anyone can see my topic.

If anybody can give help, it would be highly appreciated

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: trouble implementing ItemSend event in Outlook

Post by FanaticGuru » 08 Aug 2022, 13:27

thisiswill wrote:
04 Aug 2022, 09:24
I'm trying to write a script which would monitoring when a user sends a new email through Outlook app, and get those specific informations

Here is some example code that I already have for monitoring the EventEmail_Send event:

Code: Select all

global olEmail
olApp := ComObjActive("Outlook.Application")
olEmail := olApp.CreateItem(0)
ComObjConnect(olEmail, "EventEmail_")

olNameSpace := olApp.GetNamespace("MAPI")
olEmail.SendUsingAccount := olNameSpace.Accounts.Item("[email protected]")

olEmail.To := "[email protected]"
olEmail.Body := "this is a test"
olEmail.Subject := "COM email test with attachment"
;~ email.Attachments.Add("some full path", 1, 1, "some full path")
olEmail.Display(true)

Esc::ExitApp

EventEmail_Send(Cancel)
{
	if (olEmail.SendUsingAccount.DisplayName = "[email protected]")
	{
		MsgBox You are not authorized to send emails from: [email protected]
		return ComObject(0xB,0) ; VT_Bool False = Cancels Send with Email staying open
	}
		
	if InStr(olEmail.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
	}

}

This should point you on a path to what you seem to need.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: trouble implementing ItemSend event in Outlook

Post by FanaticGuru » 08 Aug 2022, 13:50

FanaticGuru wrote:
08 Aug 2022, 13:27
Here is some example code that I already have for monitoring the EventEmail_Send event:

I see I got another example in my collection using ItemSend event:

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
	}
}

Better and simpler than Send event for your purpose.

Send is good for watching for a specific email to be sent. ItemSend is better for watching for any email to be sent.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

thisiswill
Posts: 5
Joined: 04 Aug 2022, 08:08

Re: trouble implementing ItemSend event in Outlook

Post by thisiswill » 09 Aug 2022, 11:22

FanaticGuru wrote:
08 Aug 2022, 13:50
FanaticGuru wrote:
08 Aug 2022, 13:27
Here is some example code that I already have for monitoring the EventEmail_Send event:

I see I got another example in my collection using ItemSend event:

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
	}
}

Better and simpler than Send event for your purpose.

Send is good for watching for a specific email to be sent. ItemSend is better for watching for any email to be sent.

FG
Thank you, Fantastic Guru.

Would there be a way to return the full STMP address instead of the display name ? See, I would like to return the recipient full email address every time an email is sent, and then compare that to values stored in an .ini file.
I tried tweaking with the Recipients property but it did not seem to work for me. I looked at other posts such as this one viewtopic.php?t=45505, but I was not able to implement your code in mine.

Thank you again if you may help me.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: trouble implementing ItemSend event in Outlook  Topic is solved

Post by FanaticGuru » 09 Aug 2022, 13:03

thisiswill wrote:
09 Aug 2022, 11:22
Would there be a way to return the full STMP address instead of the display name ? See, I would like to return the recipient full email address every time an email is sent, and then compare that to values stored in an .ini file.
I tried tweaking with the Recipients property but it did not seem to work for me. I looked at other posts such as this one viewtopic.php?t=45505, but I was not able to implement your code in mine.

Below is a link to documentation for the mail item object:
https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem

On the left side if you click on 'Properties' you can see there is a lot of properties that can be accessed.

You might try olItem.To or olItem.Recipients.Item(1).Address.

Recipients returns a collection of Recipient as there can be more than one. So you have to use Item to get the first one, then Address to get the address. If you navigate around the MSDN object documentation, you can see once you get the Recipient object there is a bunch of properties that can be gotten from that: https://docs.microsoft.com/en-us/office/vba/api/outlook.recipient

If you wanted to get all the recipients' addresses, then you would need to loop through them. Your situation might never have more than one recipient so you can assume the first one or you could check if there is more than one then do something different.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

thisiswill
Posts: 5
Joined: 04 Aug 2022, 08:08

Re: trouble implementing ItemSend event in Outlook

Post by thisiswill » 09 Aug 2022, 13:45

@FanaticGuru

Thank you so much. This has solved my problem. Can I buy you a coffee !

Post Reply

Return to “Ask for Help (v1)”