Skip to main content

如何在郵件傳送後自動從已傳送郵件中刪除附件?

Author: Xiaoyang Last Modified: 2025-05-12

當傳送帶有附件的郵件時,附件會預設與郵件一起存儲到已傳送郵件資料夾中。為了減少您的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

便簽:此代碼僅適用於預設數據帳戶。