Script to save an email attachment and open with excel

Hey All,

So I’m working on a script that will save an excel attachment, open it, parse the data, and then email that data back out. Ive gotten pretty much all of it except one little part. I need a way to save the attachment to a specific folder and then open that saved attachment with excel. Does anyone have a best practice or a good way to accomplish this? My first thought was but date maybe? Ill post what I have.

Thanks!

using terms from application "Mail"
	on perform mail action with messages theMessages for rule ssMailGrabs
		-- The folder to save the attachments in (must already exist)
		
		
		-- Save in a sub-folder based on the name of the rule in Mail
		
		set subFolder to name of ssMailGrabs
		tell application "Finder"
			set attachmentsFolder to ((path to home folder as text) & "Documents:Attachments") as text
			if not (exists folder subFolder of folder attachmentsFolder) then
				make new folder at attachmentsFolder with properties {name:subFolder}
			end if
		end tell
		tell application "Mail"
			
			repeat with eachMessage in theMessages
				
				set {year:y, month:m, day:d, hours:h, minutes:min} to eachMessage's date sent
				set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min))
				
				try
					-- Save the attachment
					repeat with theAttachment in eachMessage's mail attachments
						
						set originalName to name of theAttachment
						set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & " " & originalName
						try
							save theAttachment in file (savePath)
						end try
					end repeat
					
					display dialog "A New Excel Spreadsheet has been copied"
				end try
			end repeat
			
		end tell
	end perform mail action with messages
end using terms from

-- Adds leading zeros to date components
on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

Hi There,

Welcome to MacScripter!

Not sure if you’ve sorted your script, if not hopefully you’ll be able to take something from the one below.
I don’t use Mail on my machine so it’s been written for Microsoft Outlook.

The script uses a folder on my desktop called ‘excel-docs-to-parse’ and works with a selected email.
I’ve not put it all in a loop for multiple messages, that should be pretty straight forward.


set theExcelDocsToParse to (path to home folder from user domain) & "Desktop:excel-docs-to-parse:" as string

tell application "Microsoft Outlook"
	activate
	
	set theMessageSelection to selection
	
	set theSendersName to name of (get sender of theMessageSelection)
	set theSendersEmail to address of (get sender of theMessageSelection)
	set theAttachmentsList to every attachment of theMessageSelection
	
	repeat with thisAttachment in theAttachmentsList
		save thisAttachment in theExcelDocsToParse
	end repeat
	
end tell


tell application "Finder"
	set theExcelDocsToProcess to every file of folder theExcelDocsToParse as alias list
end tell


set excelData to ""

tell application "Microsoft Excel"
	activate
	
	repeat with thisExcelDoc in theExcelDocsToProcess
		open thisExcelDoc as string
		set excelData to excelData & formula of used range of active sheet
		close front workbook saving no
	end repeat
end tell


set theSubject to "Parsed email information..."

tell application "Microsoft Outlook"
	activate
	
	set theMessage to make new outgoing message with properties {subject:theSubject, content:excelData as string}
	make new recipient at theMessage with properties {email address:{name:theSendersName, address:theSendersEmail}}
	open theMessage
	
end tell

HTH