MS Outlook COM : Find and Extract attachments from Outlook folder

Post your working scripts, libraries and tools
User avatar
Xeo786
Posts: 217
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

MS Outlook COM : Find and Extract attachments from Outlook folder

07 Jan 2020, 07:02

I wrote this code to find and extract attachments to specific folder :HeHe:

Requirment
1 . MS Outlook installed
2 . MS Outlook running, do not ask why :shifty:

How it work
1 . It look for specific subject and finds email
2 . It look for specific attachments name in found email
3 . It will not look into subfolder :problem: coz I didn't need it to :think:

How to make it work fast
1 . Make rule to save email specific email to different folder
2 . Email older then three months get deleted, coz the less the folder has will be faster to lookup
or
3 . make inverse loop so it will look for recent to past order and function `return` when first attachment found

Code: Select all

	Loop % folder.items.count
		thisattachment := attachments.Item(folder.items.count - a_index)

Code: Select all

ExtractArractmentcoutlook("Inspection Reports","337597","1543387", a_desktop)
return

ExtractAttactmentcoutlook(folderName,EmailSubject,attachmentname,PathToSaveTo)
{
	attachmentname := "i)" attachmentname
	outlook := ComObjActive("Outlook.Application")
	outlooknamespace := outlook.GetNameSpace("MAPI")
	folder := outlooknamespace.Folders(folderName)
	Loop % folder.items.count
	{
		email := folder.items.Item(A_Index)
		if(InStr(email.Subject, EmailSubject))
		{
			attachments := email.Attachments
			Loop % Attachments.Count
			{
				thisattachment := attachments.Item(A_Index)
				if RegExMatch(thisattachment.DisplayName , attachmentname )
				{
					Fullpath := pathtosaveto "\" thisattachment.DisplayName
					thisattachment.SaveAsFile(FullPath)
					return thisattachment.DisplayName ; disable this line and it will extract every attachments and same name attachment get renames, 
				}
			}
		}
	}
	msgbox, no email found Having Subject = %EmailSubject% `n specially having attachment with name = %attachmentname%
}
Plz make comments how I improve my coding or any suggestion you like, I would never mind :angel:
Good Luck :yawn:
Last edited by Xeo786 on 21 Mar 2020, 01:24, edited 1 time in total.
"When there is no gravity, there is absolute vacuum" -Game changer theory
User avatar
aeiklmr
Posts: 11
Joined: 09 Dec 2018, 22:28
Location: Hungary
Contact:

Re: MS Outlook COM : Find and Extract attachments from Outlook folder

07 Jan 2020, 10:13

I wrote a very similar script for myself in December. I sent out thousands of e-mails and I had to collect the bounced e-mails from them (old customers from our ERP system). Usually, most of the bounced e-mails contained an attachment so I had to check them. This way I could clean the e-mail list I received. Nice one, I'll try your solution next time, thanks for that!
I like to think extroverts have attention dependency disorder.
jusplayn
Posts: 1
Joined: 19 Mar 2020, 12:56

Re: MS Outlook COM : Find and Extract attachments from Outlook folder

19 Mar 2020, 13:08

Hi Xeo786,

Question, how did you get #3 to work " Make inverse loop so it will look for recent to past order and function `return` when first attachment found"? I cant seem to get that to work. Im trying to loop through the email for a specific attachment and return the most recent. If you could help, that would be great..Thanks

Xeo786 wrote:
07 Jan 2020, 07:02
I wrote this code to find and extract attachments to specific folder :HeHe:

Requirment
1 . MS Outlook installed
2 . MS Outlook running, do not ask why :shifty:

How it work
1 . It look for specific subject and finds email
2 . It look for specific attachments name in found email
3 . It will not look into subfolder :problem: coz I didn't need it to :think:

How to make it work fast
1 . Make rule to save email specific email to different folder
2 . Email older then three months get deleted, coz the less the folder has will be faster to lookup
or
3 . make inverse loop so it will look for recent to past order and function `return` when first attachment found

Code: Select all

	Loop % folder.items.count
		thisattachment := attachments.Item(folder.items.count - 1)

Code: Select all

ExtractArractmentcoutlook("Inspection Reports","337597","1543387", a_desktop)
return

ExtractAttactmentcoutlook(folderName,EmailSubject,attachmentname,PathToSaveTo)
{
	attachmentname := "i)" attachmentname
	outlook := ComObjActive("Outlook.Application")
	outlooknamespace := outlook.GetNameSpace("MAPI")
	folder := outlooknamespace.Folders(folderName)
	Loop % folder.items.count
	{
		email := folder.items.Item(A_Index)
		if(InStr(email.Subject, EmailSubject))
		{
			attachments := email.Attachments
			Loop % Attachments.Count
			{
				thisattachment := attachments.Item(A_Index)
				if RegExMatch(thisattachment.DisplayName , attachmentname )
				{
					Fullpath := pathtosaveto "\" thisattachment.DisplayName
					thisattachment.SaveAsFile(FullPath)
					return thisattachment.DisplayName ; disable this line and it will extract every attachments and same name attachment get renames, 
				}
			}
		}
	}
	msgbox, no email found Having Subject = %EmailSubject% `n specially having attachment with name = %attachmentname%
}
Plz make comments how I improve my coding or any suggestion you like, I would never mind :angel:
Good Luck :yawn:
User avatar
Xeo786
Posts: 217
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: MS Outlook COM : Find and Extract attachments from Outlook folder

21 Mar 2020, 01:23

jusplayn wrote:
19 Mar 2020, 13:08
Hi Xeo786,

Question, how did you get #3 to work " Make inverse loop so it will look for recent to past order and function `return` when first attachment found"? I cant seem to get that to work. Im trying to loop through the email for a specific attachment and return the most recent. If you could help, that would be great..Thanks
well it has to be `- a_index`

Code: Select all

	Loop % folder.items.count
		thisattachment := attachments.Item(folder.items.count - a_index)
"When there is no gravity, there is absolute vacuum" -Game changer theory

Return to “Scripts and Functions”

Who is online

Users browsing this forum: chrich and 31 guests