Skip to main content

如何在 Outlook 中選取的郵件中獲取附件資訊清單?

Author: Siluvia Last Modified: 2025-05-12

對於帶有附件的已接收郵件,有時您可能需要了解附件的詳細信息,例如顯示名稱、檔案名稱等。實際上,VBA 代碼可以幫助您輕鬆獲取 Outlook 中當前選取郵件的附件資訊清單。請瀏覽以下教程以獲取更多詳細信息。

在 Outlook 選取的郵件中獲取附件資訊清單

Office Tab - 啟用 Microsoft Office 中的分頁編輯和瀏覽功能,讓工作變得輕鬆愉快
立即解鎖 Kutools for Outlook 的免費版本,享受超過 70 種功能的無限存取權限
強化您的 Outlook 2024 - 2010 或 Outlook 365,使用這些進階功能。享受 70 多種強大功能,提升您的郵件體驗!

在 Outlook 選取的郵件中獲取附件資訊清單

1. 選擇您要獲取其附件資訊的帶有附件的郵件。

2. 按下鍵盤上的 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 視窗。

3. 然後雙擊 Project1 > Microsoft Outlook Object > ThisOutlookSession 以打開 Project1 – ThisOutlookSession 視窗。參見截圖:

the Project1 – ThisOutlookSession window

4. 然後將以下 VBA 代碼複製並粘貼到 Project1 – ThisOutlookSession 視窗中。

VBA 代碼:獲取附件資訊清單

Option Explicit
Public Sub GetAttachmentList()
    Dim selItem As Object
    Dim aMail As MailItem
    Dim aAttach As attachment
    Dim Report As String
    
    For Each selItem In Application.ActiveExplorer.Selection
        If selItem.Class = olMail Then
            Set aMail = selItem
            For Each aAttach In aMail.Attachments
                Report = Report & vbCrLf & "------------------------------------------------------------------------" & vbCrLf
                Report = Report & GetAttachmentInfo(aAttach)
            Next
            Call CreateReportEmail("Attachment Report", Report)
        End If
    Next
End Sub
 
Public Function GetAttachmentInfo(attachment As attachment)
    Dim Report
    GetAttachmentInfo = ""
    Report = Report & "Index: " & attachment.Index & vbCrLf
    Report = Report & "Display Name: " & attachment.DisplayName & vbCrLf
    Report = Report & "File Name: " & attachment.FileName & vbCrLf
    Report = Report & "Block Level: " & attachment.BlockLevel & vbCrLf
    Report = Report & "Path Name: " & attachment.PathName & vbCrLf
    Report = Report & "Position: " & attachment.Position & vbCrLf
    Report = Report & "Size: " & attachment.Size & vbCrLf
    Report = Report & "Type: " & attachment.Type & vbCrLf
    
    GetAttachmentInfo = Report
End Function
Sub CreateReportEmail(Title As String, Report As String)
    Dim aMail As MailItem
    
    Set aMail = Application.CreateItem(olMailItem)
        
    aMail.Subject = Title
    aMail.Body = Report
    
    aMail.Display
End Sub 

5. 按下鍵盤上的 F5 鍵以運行 VBA 代碼。

6. 現在會彈出一個巨集對話框,請點擊執行按鈕。

a Macros dialog box

7. 點擊巨集對話框中的執行按鈕後,將創建一個新消息窗口,其中列出所選郵件的所有附件資訊於郵件正文中。參見截圖:

a new message window is created with all attachments information of selected email listed inside the email body

注意:此 VBA 代碼適用於 Outlook 2007、2010 和 2013。