用于在本地文件夹中保存电子邮件副本的 Outlook VBA 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25039209/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook VBA macro for saving emails copies in a local folder
提问by Datageek
Whenever sending an email, I would like a copy of that email to be saved in the local folder, together with all attachments.
每当发送电子邮件时,我都希望将该电子邮件的副本以及所有附件保存在本地文件夹中。
I don't think this is possible with a custom rule in Outlook but perhaps it could be done with a VBA script?
我认为在 Outlook 中使用自定义规则是不可能的,但也许可以使用 VBA 脚本来完成?
I use Outlook and MS Exchange.
我使用 Outlook 和 MS Exchange。
回答by David Zemens
Sure it can be done using the Application_ItemSendevent procedure to call a custom procedure which will save your sent mails to a local folder.
当然可以使用Application_ItemSend事件过程调用自定义过程来完成,该过程将您发送的邮件保存到本地文件夹。
This code goes in "ThisOutlookSession" module.
此代码位于“ThisOutlookSession”模块中。
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Call SaveACopy(Item)
End Sub
Sub SaveACopy(Item As Object)
    Const olMsg As Long = 3
    Dim m As MailItem
    Dim savePath As String
    If TypeName(Item) <> "MailItem" Then Exit Sub
    Set m = Item
    savePath = "c:\users\your_user_name\desktop\"  '## Modify as needed
    savePath = savePath & m.Subject & Format(Now(), "yyyy-mm-dd-hhNNss")
    savePath = savePath & ".msg"
    m.SaveAs savePath, olMsg
End Sub
You will need to ensure that the specified path is unique/etc., the above example is fairly crude. You also need strip out any illegal characters that can't be put in a file name (slash, pipes, etc.)...
您需要确保指定的路径是唯一的/等等,上面的例子相当粗糙。您还需要删除任何不能放在文件名中的非法字符(斜杠、管道等)...
As an alternative, I would suggest simply archiving your folder(s) periodically. You can configure Outlook to save a copy of sent mail to a "Sent" folder, and then you should be able to archive that folder; saving each item individually seems less-than-optimal.
作为替代方案,我建议您定期归档您的文件夹。您可以将 Outlook 配置为将已发送邮件的副本保存到“已发送”文件夹中,然后您应该能够对该文件夹进行存档;单独保存每个项目似乎不太理想。

