Copyng an email from sent items folder into C:\Users\folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Copyng an email from sent items folder into C:\Users\folder

27 Jan 2020, 09:17

Good afternoon all,
Had a look at the forum and all the answers seem to relate to sending emails.

what I am trying to do

1. Get a specific email from the sent items (investigation reports complete.msg)
2. Hit the shortcut and it will copy this into a folder in my c drive e.g. C:\Users\JK\finished report emails

I have the first part of the code to open sent items folder. However I don't know how to select the email from this folder, then send to C drive.

Code: Select all

try oOutlook := ComObjActive("Outlook.Application")
catch {
    MsgBox, Could not find the Outlook application object.
    return
}
objFolder := oOutlook.Session.GetDefaultFolder(olFolderSentMail := 5)
oOutlook.ActiveExplorer.CurrentFolder := objFolder
objFolder := ""
oOutlook := ""

your help is appreciated as always.
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Copyng an email from sent items folder into C:\Users\folder

28 Jan 2020, 02:49

try something like, following code is not test so :mrgreen:

Code: Select all

try oOutlook := ComObjActive("Outlook.Application")
catch {
    MsgBox, Could not find the Outlook application object.
    return
}
objFolder := oOutlook.Session.GetDefaultFolder(olFolderSentMail := 5)
oOutlook.ActiveExplorer.CurrentFolder := objFolder
olItem := objFolder.items.Item(1)  ; Item(1) serves as first oldest email sent here you can use ahk loop to lookup .subject or .SenderName and catch item number
filename := olItem.Subject

filename := RegExReplace(filename,[-,0/\\:*?""<>|]) ; removing illegal chars
olItem.SaveAs("c:\" filename  ".msg")
objFolder := ""
oOutlook := ""

"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: Copyng an email from sent items folder into C:\Users\folder

28 Jan 2020, 05:55

Thanks for your reply @Xeo786,
I understand its untested.

I tried your script with two different folders and nothing happened, no email was moved into these folders.

Please note: I had to comment out the regex line because it said the \\ is an illegal character.

Apart from that the only thing I can think of is maybe I need to fill in something between the quotation marks for the below parts of the code?

objFolder := ""
oOutlook := ""


Let me know any suggestions of things to try.

Cheers
J
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Copyng an email from sent items folder into C:\Users\folder

28 Jan 2020, 06:20

JKnight_xbt33 wrote:
28 Jan 2020, 05:55
Let me know any suggestions of things to try.[/b]
Cheers
J
this is tested code, outlook gona show pop up saying allow a program to access,

Image

Code: Select all

olDoc		:=	4		;	Microsoft Office Word format (.doc)
olHTML		:=	5		;	HTML format (.html)
olICal		:=	8		;	iCal format (.ics)
olMHTML		:=	10		;	MIME HTML format (.mht)
olMSG		:=	3		;	Outlook message format (.msg)
olMSGUnicode	:=	9		;	Outlook Unicode message format (.msg)
olRTF		:=	1		;	Rich Text format (.rtf)
olTemplate	:=	2		;	Microsoft Outlook template (.oft)
olTXT		:=	0		;	Text format (.txt)
olVCal		:=	7		;	VCal format (.vcs)
olVCard		:=	6		;	VCard format (.vcf)

; https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.saveas
try oOutlook := ComObjActive("Outlook.Application")
catch {
	MsgBox, Could not find the Outlook application object.
	return
}
objFolder := oOutlook.Session.GetDefaultFolder(olFolderSentMail := 5)
oOutlook.ActiveExplorer.CurrentFolder := objFolder
olItem := objFolder.items.Item(1)  ; Item(1) serves as first oldest email sent here you can use ahk loop to lookup .subject or .SenderName and catch item number
filename := olItem.Subject
msgbox, % filename 
filename := RegExReplace(filename,"[" Chr(1) "-" Chr(31) "\\/:*?""<>|]", a_space) ; replacing illegal chars with space

olItem.SaveAs("d:\" filename  ".msg", olMSG)
objFolder := ""
oOutlook := ""
return
here is link if you need to extract attachments and save them to folder
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=71377

JKnight_xbt33 wrote:
28 Jan 2020, 05:55
Apart from that the only thing I can think of is maybe I need to fill in something between the quotation marks for the below parts of the code?

objFolder := ""
oOutlook := ""
this simply mean you empty your var
https://www.autohotkey.com/docs/Objects.htm#Usage_Freeing_Objects
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Copyng an email from sent items folder into C:\Users\folder

28 Jan 2020, 16:41

JKnight_xbt33 wrote:
27 Jan 2020, 09:17
what I am trying to do

1. Get a specific email from the sent items (investigation reports complete.msg)
2. Hit the shortcut and it will copy this into a folder in my c drive e.g. C:\Users\JK\finished report emails

Code: Select all

F5::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olFolder := olNameSpace.GetDefaultFolder(5) ; 5 = olFolderSentMail
	sSubject := "investigation reports complete"
	for olMailItem in olFolder.Items.Restrict("@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = '" sSubject "'") ; 
	{
		PathFile := "C:\Users\JK\finished report emails\" RegExReplace(olMailItem.Subject, "[\?<>/\\\*""|:]") ".msg" ; remove illegal filename characters
		while FileExist(PathFile) ; add numbers to filename if duplicate already exist
		{
			SplitPath, PathFile,, OutDir, OutExtension, OutNameNoExt
			if (A_Index = 1)
				PathFile := OutDir "\" OutNameNoExt " (" A_Index ")." OutExtension
			else
				PathFile := OutDir "\" SubStr(OutNameNoExt,1,-3) "(" A_Index ")." OutExtension
		}
		olMailItem.SaveAs(PathFile, 3)
	}
return
This looks for emails with an exact subject in the Sent Items and Saves them.

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
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: Copyng an email from sent items folder into C:\Users\folder

29 Jan 2020, 06:50

Thanks for both your scripts. I tried them and came across the following minor issues:



1. both scripts work but save the email one directory level up

For example it will save the file in Documents rather than 18-reports PDFs
C:\Users\JK\Documents\18-reports PDFs\

FYI I have shared network drive which i dont want to share on here. So rather than C:\Users really it reads xxxxxxx\Users


@FanaticGuru your script saved multiple copies of the email (same subject)

Is it possible to filter and copy over only the most recently sent one?



@Xeo786 your script copies over the oldest email.

Is it possible to copy over the newest sent? e.g. olItem := objFolder.items.Item(Newest)


Appreciate your help with this
J
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Copyng an email from sent items folder into C:\Users\folder  Topic is solved

29 Jan 2020, 15:27

JKnight_xbt33 wrote:
29 Jan 2020, 06:50

1. both scripts work but save the email one directory level up

For example it will save the file in Documents rather than 18-reports PDFs
C:\Users\JK\Documents\18-reports PDFs\

FYI I have shared network drive which i dont want to share on here. So rather than C:\Users really it reads xxxxxxx\Users


@FanaticGuru your script saved multiple copies of the email (same subject)

Is it possible to filter and copy over only the most recently sent one?

Code: Select all

F5::
	SaveFolder := "\\Shared\Users\JK\Documents\18-reports PDFs\" ; changed to the desired path
	sSubject := "investigation reports complete" ; email Subject to find
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olFolder := olNameSpace.GetDefaultFolder(5) ; 5 = olFolderSentMail
	olMailItems := olFolder.Items.Restrict("@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = '" sSubject "'") ; restrict emails to specified Subject
	olMailItems.Sort("Received", true) ; sort collection, true or false for descending or ascending
	olMailItem := olMailItems.Item(1) ; get the first item from sorted collection
	PathFile := SaveFolder "\" RegExReplace(olMailItem.Subject, "[\?<>/\\\*""|:]") ".msg" ; create full path name with illegal characters removed
	while FileExist(PathFile) ; add numbers to full path name if duplicate already exist
	{
		SplitPath, PathFile,, OutDir, OutExtension, OutNameNoExt
		if (A_Index = 1)
			PathFile := OutDir "\" OutNameNoExt " (" A_Index ")." OutExtension
		else
			PathFile := OutDir "\" SubStr(OutNameNoExt,1,-3) "(" A_Index ")." OutExtension ; increment number if a numbered file name already existed
	}
	olMailItem.SaveAs(PathFile, 3) ; save the email
return

For the code above to work the folder "18-reports PDFs" has to already exist. It would be trivial to have the script check and create the folder if needed.

Almost half of this code is just about getting a good file name, not overriding existing files, etc.

You could do all kinds of fancy naming and folder creating if you wanted. Putting the date in the name of the saved file, etc.

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
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Copyng an email from sent items folder into C:\Users\folder

30 Jan 2020, 01:47

JKnight_xbt33 wrote:
29 Jan 2020, 06:50

@Xeo786 your script copies over the oldest email.

Is it possible to copy over the newest sent? e.g. olItem := objFolder.items.Item(Newest)

Appreciate your help with this
J
this will get you newest email from folder

Code: Select all

olItem := objFolder.items.Item( objFolder.items.count ) ; newest email from folder
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: Copyng an email from sent items folder into C:\Users\folder

30 Jan 2020, 06:33

Thanks both for your final answers, they both worked.

I've chosen @FanaticGuru answer because it got exactly what i was looking for
1. the last investigation complete email
2. this time it actually copied over to the subfolder

@Xeo786 Thank you, your answer will definitely be useful in future scripts.

Appreciated both
J

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

Re: Copyng an email from sent items folder into C:\Users\folder

30 Jan 2020, 13:51

Xeo786 wrote:
30 Jan 2020, 01:47
this will get you newest email from folder

Code: Select all

olItem := objFolder.items.Item( objFolder.items.count ) ; newest email from folder

It is worth noting that 'newest' is somewhat ambiguous here. This will get the last item in the collection which is often the item with the newest receive date and time but not always. It is sufficient for most cases but the database is not always in chronological order. I have encountered cases where it is not which is when I then started using the "Sort" method.

Quote from MSDN:
"The index for the Items collection starts at 1, and the items in the Items collection object are not guaranteed to be in any particular order."

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
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Copyng an email from sent items folder into C:\Users\folder

31 Jan 2020, 00:35

FanaticGuru wrote:
30 Jan 2020, 13:51
I have encountered cases where it is not which is when I then started using the "Sort" method.

Quote from MSDN:
"The index for the Items collection starts at 1, and the items in the Items collection object are not guaranteed to be in any particular order."

FG
Fascinating..! I never had issue using max number count, I think I always had my email sorted from newest to oldest. Thanks FG :D

Code: Select all

olMailItems := olFolder.Items.Restrict("@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = '" sSubject "'") 
again thanks for code above I find it very useful but I am curious about the url will it remain same? if yes why ?
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Copyng an email from sent items folder into C:\Users\folder

03 Feb 2020, 14:22

Xeo786 wrote:
31 Jan 2020, 00:35

Code: Select all

olMailItems := olFolder.Items.Restrict("@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = '" sSubject "'") 
again thanks for code above I find it very useful but I am curious about the url will it remain same? if yes why ?

That is not a true url, that is just Microsoft's crazy naming convention to access a Namespace. It does not require an internet connection or anything like that to work. It is very unlikely to change.

Here is more information on the subject:
https://docs.microsoft.com/en-us/office/vba/outlook/how-to/navigation/referencing-properties-by-namespace

This appears to also work:
olMailItems := olFolder.Items.Restrict("@SQL=""urn:schemas:httpmail:subject"" = '" sSubject "'")

How to access these Namespaces and their properties is all pretty far down the rabbit whole of Windows inner workings.

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: JoeWinograd, yabab33299 and 123 guests