将Outlook邮件自动保存到指定的文件夹

时间:2018-03-13 07:12:06

标签: vba email outlook outlook-vba

我希望每次从特定ID(例如xyz@abc.com')收到指定文件夹时自动保存电子邮件。 然后我可以使用此文件作为我的脚本的触发器。

可以这样做吗?这需要VBA还是仅仅是前景?

如果邮件可以保存为文本文件,那就太棒了。

2 个答案:

答案 0 :(得分:2)

在Outlook中将宏安全性设置为低: 转到文件,选项,信任中心并打开信任中心设置,然后更改宏设置。在Outlook 2007及更早版本中,它位于工具,宏安全性。

打开VBA编辑器:

将代码放入模块中:

  1. 右键单击Project1并选择Insert>模块
  2. 将以下宏复制并粘贴到新模块中。

        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
    
  3. 这个网站给了我完美的答案:

    https://www.slipstick.com/outlook/archive-outlook/save-incoming-messages-hard-drive/

    只需复制粘贴代码并更改基本参数即可。 像枪一样工作。

答案 1 :(得分:-1)

您可以使用简单的脚本和规则自动保存文件夹的附件。

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