COM Outlook Events

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: COM Outlook Events

13 Nov 2017, 21:11

I think I know the solution to my problem. I'll give it a go once I have access to a computer. Thanks for your help thus far!
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: COM Outlook Events

14 Nov 2017, 02:11

ShubhamM wrote:I used .SenderEmailAddress but it still returns the exchange server string. How do I modify this to retrieve the email address itself, without the Exchange Server address?
Can't actual test it but you might try this:

Code: Select all

#F10::
	olApp := ComObjActive("Outlook.Application")
	olEmail := olApp.ActiveWindow.CurrentItem	; Expects an Email to be open
	if !(SMTP := olEmail.Sender.GetExchangeUser.PrimarySmtpAddress)
		SMTP := olEmail.SenderEmailAddress
	MsgBox % SMTP
return
FG

Edit: Fixed typo pointed out by @Coriel-11.
Last edited by FanaticGuru on 22 Jan 2021, 14:30, edited 1 time in total.
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
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: COM Outlook Events

14 Nov 2017, 12:39

Hey there, FG,

No, this one did not work. If you have any other ideas off the top of your head, then I'd really appreciate them!! If not, don't stress it and I thank you, kindly, for all your help! You are too kind and brilliant!
Guest

Re: COM Outlook Events

14 Nov 2017, 16:45

Code: Select all

#SingleInstance Force
#Persistent
olFolderSentMail := 5
olFolderItems := ComObjActive( "Outlook.Application" ).Session
    .GetDefaultFolder( olFolderSentMail ).Folders( "TestFolder" ).Items
ComObjConnect( olFolderItems, "EventFolderItems_" )
return

EventFolderItems_ItemAdd(olItem, olItems) {
    ( olRecip := olItem.Session.CreateRecipient( olItem.SenderName ) ).Resolve
    MsgBox 64
        , % "New Item in " olItems.Parent.Name
        , % olRecip.Resolved 
        ? olRecip.AddressEntry.GetExchangeUser.PrimarySmtpAddress
        : olItem.SenderEmailAddress
}
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: COM Outlook Events

15 Nov 2017, 11:12

Thanks, man! This worked! I appreciate it so much!

Just out of my curiosity, what do the "?" and ":" do in the message box or to the olRecip.Resolved variable? How do they work and how can they be used in the future? In addition, could you advise the same on ".Resolve". I thought it would be good to ask in case to learn in case something like this comes up again.
Coriel-11
Posts: 45
Joined: 18 Aug 2017, 09:02

Re: COM Outlook Events

22 Jan 2021, 12:10

FanaticGuru wrote:
14 Nov 2017, 02:11
ShubhamM wrote:I used .SenderEmailAddress but it still returns the exchange server string. How do I modify this to retrieve the email address itself, without the Exchange Server address?
Can't actual test it but you might try this:

Code: Select all

#F10::
	olApp := ComObjActive("Outlook.Application")
	olEmail := olApp.ActiveWindow.CurrentItem	; Expects an Email to be open
	if !(SMTP := oEmail.Sender.GetExchangeUser.PrimarySmtpAddress)
		SMTP := olEmail.SenderEmailAddress
	MsgBox % SMTP
return
FG
The only thing that is wrong with this one is that in line 4 olEmail has been mistyped. The 4th line should read:
if !(SMTP := olEmail.Sender.GetExchangeUser.PrimarySmtpAddress)

Matt
Stavencross
Posts: 90
Joined: 24 May 2016, 16:42

Re: COM Outlook Events

11 Sep 2021, 15:48

FanaticGuru wrote:
13 Nov 2017, 20:24
ShubhamM wrote:This worked!! Thank so much!

One small follow-up as this only covers the recipients, how do I obtain the same for the sender (who the email is from)?

Also, for the EventApp_ItemSend(Outlook_Item) [Item Send outlook event], I tried to save the email via Outlook_Item.SaveAs(Email_To_Save) but the email saves as if it hasn't been sent. It has everything filled out, but it looks like an email before you click send. How can I save this email as if it has already been sent? Is there also a way to retrieve the creation time for when this same email was generated? When I use .creationtime, it returns "1/1/1401" for this new item. I am using current date time for when it sends, but it doesn't reflect the time that the email was generated, which I prefer to have. Please advise on these if you can. Thanks a lot!
The Sender of an email can be obtained by:

Code: Select all

#F10::
	olApp := ComObjActive("Outlook.Application")
	olEmail := olApp.ActiveWindow.CurrentItem	; Expects an Email to be open
	MsgBox % olEmail.SenderEmailAddress
return
It is important to realize that an email does not have a sender until it is actually sent.

Also after an email is sent, a new email object is created so the handle that was used to access the before sending email will not work to access the after sending email.

Here is a post that talks about that some:
https://autohotkey.com/boards/viewtopic.php?p=168213#p168213

There is no easy way that I know of to go from the before send email handle to the after send email handle.

FG

I know this is a super old post, but I've read like everything you've written about outlook COM trying to dream up a solution to my question, It may not exactly be on this thread, but you previously mentioned somewhere that "You couldn't access a sent mail, because once you send the mail, it destroys that object and it becomes a new object else where."

I found the .Copy property exists in the MailItem object, and when I combine that with the _Send event;
I'm able to listen for when a specifically created email is sent, clone it, and I can do whatever I need to with it.

This answers my question " I want to pop a new email, let users modify it, then send that email to multiple places " and I thought ti might help you out a bit. Here is my example:

Code: Select all

ol := ComObjCreate("Outlook.Application")
global Email
ns := ol.getNamespace("MAPI")
ns.logon("","",true,false)		
Email := ol.CreateItem(0) ;0 is the constant to create a mailItem
emailBody:="Lorem ipsum dolor sit amet, consectetur adipiscing elit"
emailSubject:="my first subject"
emailSubject2:="my second subject"
toAddress:="chrismiller015@gmail.com"

olFormatHTML := 2 ;2 is the constant for HTML
Email.BodyFormat := olFormatHTML
Email.Subject := emailSubject
Email.HTMLBody := emailBody ;assign our HTML body
Email.To:=toAddress
Email.Display

ComObjConnect(Email,"Email_")
Email_Send(Cancel) {
	secondMail := Email.Copy
	secondMail.Display
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, Spawnova and 351 guests