MS Outlook by COM

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

MS Outlook by COM

11 May 2016, 02:20

This are how to utilize Outlook by COM, but I need to kow how to "complete" sometinghs

Code: Select all

String_To := ""
String_CC := ""
String_Subject = ""
String_HTMLBody := 
	(
	"Hello,<br>
	in <b>this</b> mail.<br><br>
	<i>bye bye<br>Luigi</i>"
	)
Attachment_1 := "C:\...\...\...\....ahk"

IfWinNotExist, ahk_class rctrl_renwnd32
{
MsgBox ,4 , ATTENTION, OUTLOOK must to be open.`nYou want to open Outlook now? 
IfMsgBox Yes
	Run C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Outlook 2010 ;your Outlook program

else
	return
Return
}
IfWinExist, ahk_class rctrl_renwnd32
{
olMailItem := 0
MailItem := ComObjActive("Outlook.Application").CreateItem(olMailItem)
MailItem.SendUsingAccount := ;must investigate
MailItem.TO := String_To
MailItem.CC := String_CC
MailItem.Subject := String_Subject
MailItem.htmlbody := String_HTMLBody ; &.htmlbody

MailItem.Attachments.Add(Attachment_1) ; Attachment_1 must be set
MailItem.Display
Sleep 200
olMailItem :=
Return
I need to understand how to select different Send Accounts (I have 3 sender account at work) and if its possible to add personal signature, normally visible in any new mail but discard after set MailItem.htmlbody :=
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

Re: MS Outlook by COM

11 May 2016, 03:48

Signature are solved

insert this and delete MailItem.htmlbody := String_HTMLBody
(from: https://autohotkey.com/board/topic/7133 ... for-ahk-l/)

Code: Select all

myInspector :=	MailItem.GetInspector, myInspector.Activate
wdDoc :=	myInspector.WordEditor
wdRange :=	wdDoc.Range(0, wdDoc.Characters.Count)
wdRange.InsertBefore(String_HTMLBody)
ps. you NOT use HTML tag for the BODY string ;)
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

Re: MS Outlook by COM

11 May 2016, 04:03

MailItem.SentOnBehalfOfName := "Insert from: other name"

But if you have more Account ... :headwall:
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

Re: MS Outlook by COM

11 May 2016, 05:12

Found:

Code: Select all

MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item(3) ; (1) or (2) or (3) ... depend by number of Account
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

Re: MS Outlook by COM

11 May 2016, 06:13

Finish work ;) :

Code: Select all

mbutton::
String_From := ""
String_To := "Test_TO"
String_CC := "Text_CC"
String_Subject = "Test_Subject"
String_HTMLBody := ("Test_body")
Attachment_1 := 0

IfWinNotExist, ahk_class rctrl_renwnd32
{
MsgBox ,4 , ATTENTION, OUTLOOK must to be open.`nYou want to open Outlook now? 
IfMsgBox Yes
	Run C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Outlook 2010 ;your Outlook program

else
	return
Return
}
IfWinExist, ahk_class rctrl_renwnd32
{
olMailItem := 0
MailItem := ComObjActive("Outlook.Application").CreateItem(olMailItem) ;create outlook mail
MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item(2) ;(1) are the standard account
MailItem.SentOnBehalfOfName := String_From ; if you have also more sender to chose
MailItem.TO := String_To
MailItem.CC := String_CC
MailItem.Subject := String_Subject
if Attachment_1 is alpha
{
	MailItem.Attachments.Add(Attachment_1) ; Attachment_1 must be set
}	
MailItem.Display
Sleep 200
myInspector :=	MailItem.GetInspector, myInspector.Activate
wdDoc :=	myInspector.WordEditor
wdRange :=	wdDoc.Range(0, wdDoc.Characters.Count)
wdRange.InsertBefore(String_HTMLBody)
olMailItem :=
Return
}
; remember to set SHOW Signature, for New message, also for other Account (if you have) 

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: MS Outlook by COM

11 Sep 2016, 21:21

BTW instead of using the Item number you can put in the email address. For instance

Code: Select all

MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item["[email protected]"] ;specifies which account
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
scriptors
Posts: 227
Joined: 25 Feb 2016, 09:01

Re: MS Outlook by COM

12 Sep 2016, 04:26

Thank you, now I not remember if try this solution (and have some problem) or not.

I'm searching how to forward message (comprehensive of attachment if present)
RonJeanJr
Posts: 20
Joined: 01 Mar 2018, 12:35

Re: MS Outlook by COM

13 Mar 2018, 08:22

While trying to create a script that uses a variable instead of a specific email send-from address via Outlook, I was able to discover that this line does NOT actually work:
MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item["[email protected]"]
I discovered this during error testing when manually setting the send-from address, in quotes, just as listed, and it completely failed to use anything other than Outlook's default sender.
This line DOES however work perfectly:
MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item(3)
Whereas (3) = the 3rd Outlook sender account

( This is the case, at least, with Outlook 2007 & AHK 1.1.28.00 )
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: MS Outlook by COM

13 Mar 2018, 13:34

scriptors wrote: if its possible to add personal signature, normally visible in any new mail but discard after set MailItem.htmlbody :=
Simple string logic will allow you to keep the current body of an email and add to it. This is very important when creating a reply and have the whole chain of emails already in the body.

Code: Select all

NewHTML:= "<p> blal blal blal </p>"
olEmail.HTMLBody := NewHTML olEmail.HTMLBody
This will make the email body equal to the NewHTML string followed by the olEmail.HTMLBody string. Effectively adding your new stuff to the top of the email.

Hooking into the wdDoc is still cool and useful but maybe simpler to just manipulate the string in most cases.

Here is an example combining both to put a table into an email:

Code: Select all

^F12::
	olApp := ComObjActive("Outlook.Application")
	olMailItem := olApp.CreateItem(0) ;  olMailItem := 0
	olMailItem.display

	olMailItem.HTMLBody := "<b>Stuff Before</b>"

	olInspector := olApp.ActiveInspector.WordEditor.Application.ActiveDocument
	olTable := olInspector.Tables.Add(olInspector.Range(olInspector.Range.End - 1), 6, 7, 1, 0) ; Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior
	olTable.Cell(1,1).Range.Text := "First Cell"
	olTable.Cell(3,2).Range.Text := "Three Down, Two Over"

	olMailItem.HTMLBody := olMailItem.HTMLBody "<i>More Stuff</i>"
return
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
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: MS Outlook by COM

14 Mar 2018, 12:56

RonJeanJr wrote:While trying to create a script that uses a variable instead of a specific email send-from address via Outlook, I was able to discover that this line does NOT actually work:
MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item["[email protected]"]
It works fine for me. I switch between multiple accounts all the time. You do need to be sure the case of your text matches exactly to your account as it is case sensitive.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
RonJeanJr
Posts: 20
Joined: 01 Mar 2018, 12:35

Re: MS Outlook by COM

02 Apr 2018, 08:31

Excellent and thank you very much - working perfectly now.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: MS Outlook by COM

02 Apr 2018, 15:57

OP already has a solution but just for generally knowledge, code like this seems strange to me:

Code: Select all

MailItem.SendUsingAccount := MailItem.Application.Session.Accounts.Item["[email protected]"]
This is taking the MailItem and working backwards to get the application object which generally the code would already have.

So you could do this:

Code: Select all

	olApp := ComObjActive("Outlook.Application")
	olEmail := olApp.ActiveWindow.CurrentItem
	olEmail.SendUsingAccount := olApp.Session.Accounts.Item("[email protected]")
Also olApp.Session is a way of getting a Namespace so I prefer using GetNamespace for that:

Code: Select all

	olApp := ComObjActive("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olEmail := olApp.ActiveWindow.CurrentItem
	olEmail.SendUsingAccount := olNameSpace.Accounts.Item("[email protected]")
The Namespace object is the term that Outlook uses and this is an object that can be used for accessing other things, like folders.

Most of this is just a combination of personal preference and common conventions but these type common conventions do make using the MSDN database somewhat easier.

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 221 guests