Outlook COM (can not send email on background)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zuzu_kuc
Posts: 76
Joined: 30 Mar 2016, 12:36

Outlook COM (can not send email on background)

Post by zuzu_kuc » 30 Sep 2022, 14:42

Hi,
i have this code, i copied data from Excel and then i want to paste them into the email body with signature under the text
and i want to keep formating from Excel.
Everything works fine, but the last command Ol.send works only if Ol.Display is true, but i want to send this email on background.
if i remove part of inspector and wordEditor and just put some text in body, then outlook send email on background but dont send the email if i use word Editor :/
Any tips please?
Thank you!

Code: Select all

path1 := "f:\template\test_data_querry_template.xlsx"
XL := ComObjCreate("Excel.Application")
XL.Visible := 0
XL.DisplayAlerts := False
XL.Workbooks.Open(Path1)
XL.ActiveWorkbook.RefreshAll
sleep, 200
XL.ActiveSheet.UsedRange.copy
sleep, 300

XL.ActiveWorkbook.Close(False)
XL.Quit()
XL := ""


Ol := ComObjCreate("Outlook.Application").CreateItem(0)

Ol.Subject := "novy mail"
Ol.to := "[email protected]"
;Ol.HTMLBody := Clipboard ; this works fine but i will lose formating (same if i use just Ol.body = )

Ol.Display ; without this i cant send email
myInspector := Ol.GetInspector
wdDoc := myInspector.WordEditor
wdRange := wdDoc.Range(0, wdDoc.Characters.Count)
wdRange.InsertBefore("")
wdRange.paste
Ol.Send

return

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Outlook COM (can not send email on background)

Post by flyingDman » 30 Sep 2022, 15:22

This post might help viewtopic.php?f=76&t=103946&p=461874&hilit=xl.ActiveWorkbook.PublishObjects.Add#p461669 .
To recap (for one range):

Code: Select all

Ol := ComObjCreate("Outlook.Application").CreateItem(0)
xl := ComObjActive("excel.application")
rng := xl.activesheet.usedrange.address()
xl.ActiveWorkbook.PublishObjects.Add(4, A_Temp "\xl2html.htm", "Sheet1", rng, 0).publish(1)      
;~ xlSourceRange=4 xlHtmlStatic=0
fileread, html, % A_temp "\xl2html.htm"
htmlarr := strsplit(html,["<body>","</body>"])
grtng := "<p>Dear xxx,</p>"
intro := "<p>Please see below the recent yyy:</p>"
slttn := "<p>Kind regards,</p><br>John Doe"
html := htmlarr.1 "<body>" grtng . intro . htmlarr.2 . slttn . "</body>" . htmlarr.3
Ol.BodyFormat := 2
Ol.To := "[email protected]"
Ol.Subject := "Testing"
Ol.HTMLBody := 	html
Ol.send
return
see here: https://learn.microsoft.com/en-us/office/vba/api/excel.publishobjects.add
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”