Open a Mail item and create a Contact item using Outlook COM calls Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
JoeWinograd
Posts: 1369
Joined: 10 Feb 2014, 20:00

Open a Mail item and create a Contact item using Outlook COM calls

12 Mar 2018, 18:30

Hi Outlook COM experts,

I'm trying to create a script that retrieves the sender's name and email address from a selected or opened mail in Outlook (2016, if that matters) and then creates a new Contact with that info. I'm using the code from the GitHub posted by kon and have come up with this so far:

Code: Select all

olApp:=ComObjCreate("Outlook.Application")  ; Create an application object
olMail:=olApp.GetActiveItem(olMailItem)  ; *** I know this is wrong - what should it be? ***
ContactEmail:=olMail.SenderEmailAddress ; get sender's email address
ContactName:=olMail.SenderName ; get sender's name
olContact:=olApp.CreateItem(olContactItem)  ; Create a new Contact
olContact.Email1Address:=ContactEmail ; put sender's email address in Email1 field
olContact.FullName:=ContactName ; put sender's name in FullName field
I'm pretty confident of most of the code (got the property names from the MSDN doc), but as noted in the comment on the second line, I know it is wrong. What is the proper call to create a mail object of a selected or opened email such that the olMail.SenderEmailAddress and olMail.SenderName assignment statements work? Also, do you see anything else wrong with any of the code? Thanks much, Joe
FanaticGuru
Posts: 1397
Joined: 30 Sep 2013, 22:25

Re: Open a Mail item and create a Contact item using Outlook COM calls  Topic is solved

13 Mar 2018, 00:35

Here is how you get the current item, either open or selected.

Code: Select all

F12::
	olApp := ComObjActive("Outlook.Application")
	try
		olItem := olApp.ActiveWindow.CurrentItem
	catch
		olItem := olApp.ActiveExplorer.Selection.Item(1)
	MsgBox % olItem.Subject "`tClass = " olItem.Class
return

; Class
; 26 = olAppointment
; 40 = olContact
; 43 = olMail
; 48 = olTask
You might do a check here to make sure you have a mail item and not a task or some other type of item selected.

Here is how you create a contact.

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olContact := olApp.CreateItem(2)	; olContactItem := 2
olContact.FirstName := "Fanatic"
olContact.LastName := "Guru"
olContact.MobileTelephoneNumber := "(123) 456-7890"

olContact.Display ; Remove this line to have it all happen in the background
;~ olContact.Save ; Uncomment to automatically save
Hopefully you can combine the two to get what you need.

Your main problem is that you can not use VBA constants olMailItem and olContactItem. Those are predefined variables within VBA.

If you just google something like "VBA Outlook constants" you should be able to find a list of all the constants and their values.

Googling "MSDN Outlook" you can probably find all the Outlook properties and methods that you can use.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
JoeWinograd
Posts: 1369
Joined: 10 Feb 2014, 20:00

Re: Open a Mail item and create a Contact item using Outlook COM calls

13 Mar 2018, 02:07

Hi FG,
Absolutely perfect! No problem combining the two pieces into the script that I need — it is working beautifully! Thanks, too, for the tip on VBA constants. Regards, Joe
User avatar
JoeWinograd
Posts: 1369
Joined: 10 Feb 2014, 20:00

Re: Open a Mail item and create a Contact item using Outlook COM calls

13 Mar 2018, 02:40

Btw, meant to say that the try-catch is a very clever way of handling either an opened or selected message — nicely done!
Last edited by JoeWinograd on 15 Mar 2018, 01:57, edited 1 time in total.
User avatar
JoeWinograd
Posts: 1369
Joined: 10 Feb 2014, 20:00

Re: Open a Mail item and create a Contact item using Outlook COM calls

15 Mar 2018, 01:56

Hi FG,
I incorporated everything into a script (about 100 lines) and it is working great with a hard-coded hotkey. I decided to take it to the next level by adding a "Settings..." choice in the context menu to allow setting the hotkey. I did it via a GUI and everything in the GUI is working fine (more than 400 lines in the script now). I'm able to create the dynamic hotkey string, such as !^F3, with no problem via the GUI. Then the Hotkey,%NewHotkey%,CreateContact command runs fine (ErrorLevel=0), but the code at the CreateContact label never executes!

I posted this issue in a new thread here:
https://autohotkey.com/boards/viewtopic.php?f=5&t=45637

I'll be most grateful if you can take a look at that and let me know if you have any ideas...or do it right here, if you prefer. Thanks again, Joe
jbreedlove
Posts: 1
Joined: 23 Apr 2019, 08:06

Re: Open a Mail item and create a Contact item using Outlook COM calls

23 Apr 2019, 08:30

Does anyone have a complete script that pieces these together to make a script to create a contact from an email? I have not been able to combine the pieces above to work.

Thanks.
FanaticGuru
Posts: 1397
Joined: 30 Sep 2013, 22:25

Re: Open a Mail item and create a Contact item using Outlook COM calls

23 Apr 2019, 15:18

jbreedlove wrote:
23 Apr 2019, 08:30
Does anyone have a complete script that pieces these together to make a script to create a contact from an email? I have not been able to combine the pieces above to work.

Here is the basics:

Code: Select all

F12::
	olApp := ComObjActive("Outlook.Application")
	try
		olItem := olApp.ActiveWindow.CurrentItem
	catch
		olItem := olApp.ActiveExplorer.Selection.Item(1)
	if (olItem.class <> 43)
		return
	else
		olMail := olItem
	olContact := olApp.CreateItem(2)	; olContactItem := 2
	olContact.FullName := olMail.SenderName
	olContact.Email1Address := olMail.SenderEmailAddress
	olContact.Email1DisplayName := olMail.SenderName "<" olMail.SenderEmailAddress ">"
	olContact.Display ; Remove this line to have it all happen in the background
	;~ olContact.Save ; Uncomment to automatically save
return
A contact item has literally dozens of properties that you could try to pull out of an email to propagate.

https://docs.microsoft.com/en-us/office/vba/api/outlook.contactitem

If I really had a need for it, I would use a regex to look for an address and phone number in the emails body starting at the bottom (typical signature). Maybe even look for company and title info.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: f1ster, flyingDman, lakozyin and 234 guests