Email Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Email

11 Jan 2018, 22:51

Hey guys,

I am trying to find out how I can send the email by using AHK. I am able to create the VBA in excel, and then send the email. (Please refer to below script) Do you know how I can embed this script into AHK, so I could use AHK to send emails?

/*VBA - Send emails*/
Dim objOutlook As Object
Dim objMail As Object

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With objMail
.to = TO_text
.cc = CC_text
.bcc = BCC_text
.Subject = Sbj_text
.Body = body_text
.Attachments.Add (path_text)
.Send 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing

End Sub

Thanks in advance,
Tina
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Email

12 Jan 2018, 00:23

WhiteSubway wrote:Hey guys,

I am trying to find out how I can send the email by using AHK. I am able to create the VBA in excel, and then send the email. (Please refer to below script) Do you know how I can embed this script into AHK, so I could use AHK to send emails?

/*VBA - Send emails*/
Dim objOutlook As Object
Dim objMail As Object

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With objMail
.to = TO_text
.cc = CC_text
.bcc = BCC_text
.Subject = Sbj_text
.Body = body_text
.Attachments.Add (path_text)
.Send 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing

End Sub

Thanks in advance,
Tina
AHK and VBA are accessing the same API of Outlook so you do things very similar just with some syntax differences between the languages.

Code: Select all

objOutlook := ComObjCreate("Outlook.Application")
objMail := objOutlook.CreateItem(0)
objMail.to := TO_text
objMail.cc := CC_text
objMail.bcc := BCC_text
objMail.Subject := Sbj_text
objMail.Body := body_text
objMail.Attachments.Add(path_text)
objMail.Display
;~ objMail.Save
;~ objMail.Send
A little simpler because AHK is looser with variable data types but on the other hand AHK does not have the With command.

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: Email

12 Jan 2018, 00:55

A more robust example:

Code: Select all

Account := "[email protected]"
Recipients := ["Them <[email protected]>", "We <[email protected]>"]
RecipientsCC := ["Us <[email protected]>", "Those <[email protected]>"]
RecipientsBCC := ["Sneaky <[email protected]>", "Very Sneaky <[email protected]>"]
Subject := "Reports"
Body = 
(Join
<a href="http://www.ahkscript.org/">http://www.ahkscript.org/</a><br>
<img src="%A_Desktop%\Test\TestPicture.jpg" />
)

for Index, Recipient in Recipients ; Conditional Body based on Recipients
	if Recipient ~= "Them"
		{
			Body = 
			(Join
			%Body%
			<br><a href="http://www.ahkscript.org/them">http://www.ahkscript.org/them</a>
			)
			break
		}

;example of creating a MailItem and setting it's format to HTML
olApp := ComObjCreate("Outlook.Application")
olEmail := olApp.CreateItem(0)	; olMailItem := 0
olNameSpace := olApp.GetNamespace("MAPI")
olEmail.SendUsingAccount := olNameSpace.Accounts.Item(Account)
olEmail.BodyFormat := 2	; olFormatHTML := 2
olEmail.Subject := Subject
olEmail.HTMLBody := Body
for Index, Recipient in Recipients
	olRecipient := olEmail.Recipients.Add(Recipient), olRecipient.Type := 1 ; To: CC: = 2 BCC: = 3
for Index, RecipientCC in RecipientsCC
	olRecipient := olEmail.Recipients.Add(RecipientCC), olRecipient.Type := 2 ; To: CC: = 2 BCC: = 3
for Index, RecipientBCC in RecipientsBCC
	olRecipient := olEmail.Recipients.Add(RecipientBCC), olRecipient.Type := 3 ; To: CC: = 2 BCC: = 3
olEmail.Attachments.Add(A_Desktop "\Test\Slinky.jpg")

olEmail.Display
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
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Re: Email

14 Jan 2018, 22:37

it works now! You are amazing!

Thanks
Tina
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Re: Email

14 Jan 2018, 23:33

Hi guys,

I guess I have one more question as you guys are amazing!

I was wondering how I can read the 'unread' emails (including the subject and the body) by using AHK. I've found below VBA script, but I still have the issue to embed it with AHK.

/* To read the body & subject of the email*/

Private WithEvents myOlItems As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

Dim Msg As Outlook.MailItem

If TypeName(item) = "MailItem" Then
Set Msg = item

MsgBox Msg.Subject
MsgBox Msg.Body

End If

ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

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

Re: Email  Topic is solved

15 Jan 2018, 15:38

WhiteSubway wrote:I was wondering how I can read the 'unread' emails (including the subject and the body) by using AHK. I've found below VBA script, but I still have the issue to embed it with AHK.
This is fairly simple if you know the structure of the object.

Code: Select all

olApp := ComObjActive("Outlook.Application")		; Use Create if Outlook is not going to be open
olNameSpace := olApp.GetNamespace("MAPI")
olFolder := olNameSpace.Folders("My Account Name").Folders("Inbox") ; Get the Folder you want going down as many subfolders as needed
olItems := olFolder.Items.Restrict("[Unread]=true") ; Restrict to only Unread
olItems.Sort("Received", true)	; true or false for descending or ascending
for olItem in olItems
	MsgBox % olItem.Subject "`n" olItem.Body	; Any valid Item property will work here
You get any Folder you want from the NameSpace. The NameSpace is basically the tree structure you normally see to the left in Outlook.

Once you have the Folder you want you get the Items from the Folder with an "Unread" Restrict or what ever status you want, then I like to Sort the Items by Received because they are not always stored internal in the order you might expect.

Then loop through and do what you want with each email.

Also I showed how to pick a specific folder but you can also use a DefaultFolder like in your VBA code.
Change the Folder line to something like this:
olFolder := olNameSpace.GetDefaultFolder(6) ; 6 = olFolderInbox

VBA has the constants built in but with AHK you have to put the actual value.

Below is a list of the default folder values:

Constant Value
olFolderCalendar 9
olFolderContacts 10
olFolderDeletedItems 3
olFolderDrafts 16
olFolderInbox 6
olFolderJournal 11
olFolderJunk 23
olFolderNotes 12
olFolderOutbox 4
olFolderSentMail 5
olFolderTasks 13
olPublicFoldersAllPublicFolders 18
olFolderConflicts 19
olFolderLocalFailures 21
olFolderServerFailures 22
olFolderSyncIssues 20

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
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Re: Email

15 Jan 2018, 21:32

Hi guys,

I've seen this post showing how to get the e-mail date and time from Outlook.

https://autohotkey.com/board/topic/1153 ... m-outlook/

I am trying to add it to the previous script (which FanaticGuru helped me come with.), so I can get the time & the date of the email.

Could you please a bit assistance again?

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

Re: Email

16 Jan 2018, 01:50

WhiteSubway wrote:Hi guys,

I've seen this post showing how to get the e-mail date and time from Outlook.

https://autohotkey.com/board/topic/1153 ... m-outlook/

I am trying to add it to the previous script (which FanaticGuru helped me come with.), so I can get the time & the date of the email.

Could you please a bit assistance again?

Thanks
Tina
Once you get the olItem in my examples above you can get any of the other properties of an email object the same as you do Subject and Body.

Here is a link to the mailitem object:
https://msdn.microsoft.com/en-us/vba/ou ... ct-outlook

You can use any of the Properties at the bottom. You probably want olItem.ReceivedTime

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
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Re: Email

16 Jan 2018, 18:20

Thanks again! I think that's what exactly I am looking for.

Cheers,
Tina
WhiteSubway
Posts: 7
Joined: 22 Aug 2017, 20:11

Re: Email

14 Mar 2018, 23:17

Hi guys,

I am seeking for a big help here again. I am trying to figure it out myself but it's really scratch my head.

I am trying to document the main process in the plain English. Could someone help me build up in AHK language which will be compatible with our other applications?

Thanks muchly,
Tina

Process
1) Login to outlook (application)

2) Make sure the outlook is open correctly
*If the user can see ‘Inbox – [email protected]’ - Outlook on the top of the page

3) Scroll down to the SHR PICS email box

4) Click the SHR PICS email box name and make sure it is the correct shared email box. * If the user can see ‘SHR PICS – Outlook’ on the top of the page

5) Check if there are any new emails in the targeted email box

6) Check if the new emails are from the internal senders (e.g. [email protected])

7) If the emails are met above 2 conditions, then send an auto reply with ‘Thanks for your email. We will review your request & response to you as soon as possible’ by clicking reply button.

8) After the email has been sent out, move the emails into ‘Working in Process’ folder under the user email account (e.g. [email protected]) by right clicking ‘move’ and selecting the destination folder.

9) If the emails are not met condition 6), then send an auto reply with ‘Sorry, we cannot accept your request at the current stage. Please ring 123 for the general questions….’
TheGGG

Re: Email

14 Apr 2018, 12:04

Hi

I´am pretty new on this - and also very interested in making a script that allows me to get the e-mail date and time from Outlook. My mission is to copy e-mails in Outlook and paste the files (.msg) into a folder in Onedrive and with an AutoHotkey rename and organize by adding date and time as follow: "2018-04-14 18.58 - Mail". I am capable of pasting in current time or file creation time - but this I can´t solve by my self. Can anybody help me getting further with a script :)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Email

16 Apr 2018, 18:17

TheGGG wrote:Hi

I´am pretty new on this - and also very interested in making a script that allows me to get the e-mail date and time from Outlook. My mission is to copy e-mails in Outlook and paste the files (.msg) into a folder in Onedrive and with an AutoHotkey rename and organize by adding date and time as follow: "2018-04-14 18.58 - Mail". I am capable of pasting in current time or file creation time - but this I can´t solve by my self. Can anybody help me getting further with a script :)
Here is an exmaple script showing how to save a selected or open email to a msg file.

Code: Select all

F12::
	olApp := ComObjActive("Outlook.Application")
	try
		olItem := olApp.ActiveWindow.CurrentItem
	catch
		olItem := olApp.ActiveExplorer.Selection.Item(1)
	if (olItem.Class = 43)
		olMailItem := olItem
	else
	{
		MsgBox Mail Item Not Current or Selected
		return
	}
	FilePathName := A_ScriptDir "\" RegExReplace(olMailItem.Subject, "[\?<>/\\\*""|:]") ".msg"
	olMailItem.SaveAs(FilePathName, 3) ; 3 = SaveAs msg file format
return
You can play around with making the FilePathName whatever and whereever you want to save the msg file. The example saves it to a file in the scripts directory with the same name as the subject of the email.

It is important to make sure the path/name does not contain any invalid characters which is what the RegExReplace does in my example. It strips out specific invalid characters.

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
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Email

22 Jul 2020, 02:33

WhiteSubway wrote:
14 Mar 2018, 23:17
Hi guys,

I am seeking for a big help here again. I am trying to figure it out myself but it's really scratch my head.

I am trying to document the main process in the plain English. Could someone help me build up in AHK language which will be compatible with our other applications?

Thanks muchly,
Tina
[...]
OK, that (unanswered) request is about 2yrs old. Haven't there been Outlook's filter-options already available? Well, I do think so. :think:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, RandomBoy and 340 guests