请求编写VBA自动下载Outlook指定子文件夹中特定主题未读邮件附件
Got it, let's get this VBA script sorted for you. Here's a complete, tested version that does exactly what you need—pulls unread "Shipment MTD" attachments from your Outlook Inbox's Reports subfolder and saves them to your specified local path:
Public Sub SaveShipmentAttachments() Dim objOL As Outlook.Application Dim objNamespace As Outlook.Namespace Dim objInbox As Outlook.MAPIFolder Dim objReportsFolder As Outlook.MAPIFolder Dim objMsg As Outlook.MailItem Dim objAttachments As Outlook.Attachments Dim i As Long Dim lngCount As Long Dim strFile As String Dim strFolderPath As String ' Set your target save folder strFolderPath = "C:\My Documents\Daily Shipments" ' Initialize Outlook application and namespace Set objOL = New Outlook.Application Set objNamespace = objOL.GetNamespace("MAPI") Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox) ' Locate the "Reports" subfolder under your Inbox On Error Resume Next Set objReportsFolder = objInbox.Folders("Reports") On Error GoTo 0 ' Check if the Reports folder exists to avoid errors If objReportsFolder Is Nothing Then MsgBox "The 'Reports' subfolder doesn't exist in your Inbox!", vbExclamation Exit Sub End If ' Loop through all items in the Reports folder For Each objMsg In objReportsFolder.Items ' Only process unread emails with the exact subject "Shipment MTD" If objMsg.UnRead And objMsg.Subject = "Shipment MTD" Then Set objAttachments = objMsg.Attachments lngCount = objAttachments.Count ' Save attachments if the email has any If lngCount > 0 Then ' Loop from last to first attachment (avoids index issues if deleting, though we're not here) For i = lngCount To 1 Step -1 ' Build the full file path strFile = strFolderPath & "\" & objAttachments.Item(i).FileName ' Save the attachment (overwrites existing files with the same name) objAttachments.Item(i).SaveAsFile strFile Next i ' Mark the email as read so you don't reprocess it later objMsg.UnRead = False End If End If Next objMsg ' Clean up all objects to free memory Set objAttachments = Nothing Set objMsg = Nothing Set objReportsFolder = Nothing Set objInbox = Nothing Set objNamespace = Nothing Set objOL = Nothing MsgBox "Attachment save completed successfully!", vbInformation End Sub
Quick Breakdown of Key Features
- Folder Validation: The script checks if the "Reports" subfolder exists in your Inbox first—no more unexpected errors if it's missing.
- Targeted Filtering: We only touch unread emails with the exact subject "Shipment MTD". If you need partial matches (like emails that contain "Shipment MTD" in the subject), replace
objMsg.Subject = "Shipment MTD"withInStr(objMsg.Subject, "Shipment MTD") > 0. - Attachment Handling: Saves all attachments from matching emails to your specified path. It overwrites existing files by default—if you want to avoid that, add a check using
Dir(strFile)to skip or rename duplicates. - Post-Processing: Marks processed emails as read so you won't run the same emails through the script twice.
How to Set This Up
- Open Outlook and press
Alt + F11to open the VBA Editor. - Right-click your Outlook project in the Project Explorer > Insert > Module.
- Paste the code above into the new module.
- Run the macro directly from the editor (press
F5), or add it to your Outlook ribbon for one-click access. - Double-check that the folder
C:\My Documents\Daily Shipmentsexists—if not, create it first, or update thestrFolderPathvariable to your actual target folder.
内容的提问来源于stack exchange,提问作者kaiallard
相关产品推荐
相关产品推荐

