跳至主要内容

如何在邮件发送后自动从“已发送邮件”中删除附件?

Author: Xiaoyang Last Modified: 2025-05-07

当发送带有附件的邮件时,默认情况下,这些附件会随邮件一起存储到“已发送邮件”文件夹中。为了减小PST文件的大小,您可能希望在邮件发送后自动从“已发送邮件”中删除附件。本文将介绍如何在Outlook中完成此任务。

在邮件发送后自动从“已发送邮件”中删除附件


在邮件发送后自动从“已发送邮件”中删除附件

以下VBA代码可以帮助您在邮件发送后自动删除Outlook中的附件,请按照以下步骤操作:

1. 按住“ALT” + F11”键打开“Microsoft Visual Basic for Applications”窗口。

2. 在“Microsoft Visual Basic for Applications”窗口中,双击“Project1(VbaProject.OTM)”窗格中的“ThisOutlookSession”以打开模块,然后将以下代码复制并粘贴到空白模块中。

VBA代码:在邮件发送后自动从“已发送邮件”中删除附件:

Public WithEvents SentMailItems As Outlook.Items
    Private Sub Application_Startup()
    Set SentMailItems = Outlook.Application.Session.GetDefaultFolder(olFolderSentMail).Items
    End Sub
    Sub SentMailItems_ItemAdd(ByVal Item As Object)
    Dim xSentMail As Outlook.MailItem
    Dim xAttachments As Outlook.Attachments
    Dim xAttachment As Outlook.Attachment
    Dim xAttachmentInfo As String
    On Error Resume Next
    If Item.Class = olMail Then
       Set xSentMail = Item
    End If
    Set xAttachments = xSentMail.Attachments
    For i = xAttachments.Count To 1 Step -1
        Set xAttachment = xAttachments.Item(i)
        xAttachmentInfo = "<HTML><BODY>" & xAttachment.DisplayName & _
                          "</BODY></HTML>" & vbCrLf & xAttachmentInfo
        xAttachment.Delete
    Next
    xSentMail.HTMLBody = "<HTML><BODY><font color=#FF0000>Attachment Removed: </font><br/></BODY></HTML>" & _
                         xAttachmentInfo & "<HTML><BODY><br/></BODY></HTML>" & xSentMail.HTMLBody
    xSentMail.Save
End Sub
the screenshot of step about using vba to automatically remove the attachments after emails sending from the sent items 1

3. 插入上述代码后,请重新启动Outlook以使代码生效。

4. 从现在开始,当您发送带有附件的邮件时,附件将自动从“已发送邮件”中删除,如下图所示:

the screenshot of step about using vba to automatically remove the attachments after emails sending from the sent items 2

注意:此代码仅适用于默认数据账户。