问题
I would like to automatically save emails every time I receive from a particular ID (ex- 'xyz@abc.com') into a specified folder. I can then use this file as a trigger for my script.
Can this be done? Will this need VBA or just outlook?
It would be great if the mail can be saved as a text file.
回答1:
Set Macro security low in your Outlook: go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
Open the VBA Editor:
To put the code in a module:
- Right click on Project1 and choose Insert > Module
Copy and paste the below macro into the new module.
Public Sub SaveMsg(Item As Outlook.MailItem) Dim sPath As String Dim dtDate As Date Dim sName As String Dim enviro As String enviro = CStr(Environ("USERPROFILE")) sName = Item.Subject ReplaceCharsForFileName sName, "_" dtDate = Item.ReceivedTime sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _ vbUseSystem) & Format(dtDate, "-hhnnss", _ vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg" ' use My Documents in older Windows. sPath = enviro & "\Documents\" Debug.Print sPath & sName Item.SaveAs sPath & sName, olMsg End Sub Private Sub ReplaceCharsForFileName(sName As String, _ sChr As String _ ) sName = Replace(sName, "/", sChr) sName = Replace(sName, "\", sChr) sName = Replace(sName, ":", sChr) sName = Replace(sName, "?", sChr) sName = Replace(sName, Chr(34), sChr) sName = Replace(sName, "<", sChr) sName = Replace(sName, ">", sChr) sName = Replace(sName, "|", sChr) End Sub
This website gave me the perfect answer:
https://www.slipstick.com/outlook/archive-outlook/save-incoming-messages-hard-drive/
Just had to copy paste their code and change the basic parameters. Works like gun.
回答2:
You can use a simple script, as well as rules, to auto-save attachments to folders.
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Users\DT168\Documents\outlook-attachments\"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub
https://www.extendoffice.com/documents/outlook/3747-outlook-auto-download-save-attachments-to-folder.html
来源:https://stackoverflow.com/questions/49250227/save-outlook-mail-automatically-to-a-specified-folder