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
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
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
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
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.